Consider the following program and predict the output:
class Main implements Runnable { public void run() { System.out.println(Thread.currentThread().getName()); }//from w ww . j a v a 2 s . co m public static void main(String arg[]) { Thread thread = new Thread(new Main()); thread.run(); thread.run(); thread.start(); } }
a)main/*from ww w.j av a 2 s .c om*/ main Thread-0 b)Thread-0 main Thread-1 c)main Thread-0 Thread-1 d)Thread-0 Thread-1 Thread-2
a)
Calling run()
directly will not create a new thread.
The correct way is to call the start()
method, which in turn will call the run()
method in a new thread.