Imsa
1
Q:

java thread

// Copy and test
// They run simultaneously

public static void main(String[] args) {
  		// How to create a thread
        Thread thread = new Thread(new Runnable() {
            @Override
			// Loop running in thread
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println("Printing plus " + i + " in a worker thread.");
                    try {
                        Thread.sleep(1000);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
  		// Loop running in main thread 
        for (int j = 0; j < 20 ; j++) {
            System.out.println("Printing plus " + j + " in a main thread.");
            try {
                Thread.sleep(900);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        }
0
public class ClassName extends Thread{
  public void run(){
    super.run();
    //Your Code
  }
}

public class Main{
  public static void main(String[] args){
    ClassName thread = new ClassName();
    thread.start();
  }
}
5

New to Communities?

Join the community