I am reading the Oracle SE 7 Certified Exams book for programmers 1Z0-804 and 1Z0-805 . One of the questions is asking for the output of this code.
class ThreadTest { public static void main(String []args) throws InterruptedException { Thread t1 = new Thread() { public void run() { System.out.print("t1 "); } }; Thread t2 = new Thread() { public void run() { System.out.print("t2 "); } }; t1.start(); t1.sleep(5000); t2.start(); t2.sleep(5000); System.out.println("main "); } }
The book says that it will always output t1 t2 main, because TIMED_WAITING state can only reach from RUNNABLE .
t1 t2 main
But I thought that the thread could exit the RUNNABLE state without following any instructions.
Doc says:
A thread in runnable state is running in a Java virtual machine, but it can expect other resources from the operating system, such as a processor.
? RUNNABLE, ?
, , .
, . RUNNABLE - , . (, , , BLOCKED) run() ( , TERMINATED). , -, .
run()
- , ; public void run() {} : return. return, RUNNABLE TERMINATED.
public void run() {}
return
(). " " 5 , . , - .
:
"main "
, !