1 Public Static void Main (string[] args)throws Exception {2 new Thread (new myrunnable ("Hello1")); 3 Th.start (); 4 SYSTEM.OUT.PRINTLN ("Current main thread" +5 System.out.println ("the thread" +Th.getname ()); 6 }
The above code, in the memory state as shown. The following steps are performed primarily in the four step below.
1, run the above Java file, the virtual machine executes the class's entry main function, assigned a main thread.
2, the main method in the main thread in the stack, the stack, the instance of a new sub-thread, and opened the child thread
3. The JVM assigns a sub-line stacks to the child thread, and executes the run () stack in this sub-thread stack.
4, by the CPU scheduling each thread, allocated to each thread of time, determined by the CPU, there are two modes, tick scheduling and preemption scheduling.
~ Time-sharing scheduling
All threads take turns using the CPU usage, averaging the time each thread consumes the CPU.
-Preemptive scheduling
Prioritize high-priority threads to use the CPU, and if the threads have the same priority, then randomly select one (thread randomness), which Java uses for preemptive scheduling.
How thread series one thread works