Here is a multi-threaded HelloWorld:
public class HelloWorld {
public static void main(String[] args) throws InterruptedException {
Thread myThread = new Thread() {
public void run() {
System.out.println("Hello World from new thread");
}
};
myThread.start();
Thread.yield();
System.out.println("Hello from main thread");
myThread.join();
}
}
As I understand, after the myThread.start()
, there will be two threads running. One is the main thread, and the other is the newly-created myThread
. Then, which thread is referred to in the Thread.yield()
?
I checked the Java SE6 Doc, which says
Thread.yield(): Causes the currently executing thread object to temporarily pause and allow other threads to execute
But in the codes, I can't see clearly what the currently excuting thread
is, it looks that both threads are running at the same time.
Isn't it be more clear to say myThread.yield()
instead of Thread.yield()
? Does anyone have ideas about this?