What is the output of the following code?
class Main extends Thread { public void run() { this.yield(); //line1 for (int i = 0; i < 1000; i++) System.out.print(i);// w w w . ja v a 2s.c o m } public static void main(String... args) { Thread myThread = new Main(); myThread.run(); //line2 } }
a
The code at line 2 doesn't start a new thread of execution.
So myThread.run()
executes in the main thread and not a separate thread.
When main executes this.yield()
(yield()
is a static method), it might make main give up its execution slot and wait until the scheduler allows it to run again.