Analysis of thread hibernation usage for Java thread scheduling

Source: Internet
Author: User

This article illustrates the thread hibernation usage of Java thread scheduling. Share to everyone for your reference. The specific analysis is as follows:

Java thread scheduling is the core of Java multithreading, only good scheduling, can give full play to the performance of the system, improve the execution efficiency of the program.

The point here is to be clear, no matter how programmers write scheduling, can only maximize the impact of thread execution order, but not accurate control.

The purpose of thread hibernation is one of the easiest ways for a thread to give up the CPU, when the thread sleeps, it gives the CPU resources to other threads so that it can rotate, and when it sleeps for a certain time, the line Cheng wakes up and waits for execution.

The method of thread hibernation is thread.sleep (long Millis) and Thread.Sleep (long millis, int nanos), all static methods, which thread that calls sleep hibernation? Simply, which thread calls sleeping, Which thread to hibernate on.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The 23 24 25 26 27 28 29 30 31 32 33 34 35 36 /** * java thread: Thread scheduling-Hibernate * * @author leizhimin 2009-11-4 9:02:40/public class Test {public static void main (string[) args ) {Thread T1 = new MyThread1 (); Thread t2 = new Thread (new myrunnable ()); T1.start (); T2.start (); } class MyThread1 extends thread {public void run () {to (int i = 0; i < 3; i++) {System.out.println ("Thread 1" + i + "The second execution!" "); try {thread.sleep (m);} catch (Interruptedexception e) {e.printstacktrace ()}} } class Myrunnable implements Runnable {public void run () {for (int i = 0; i < 3; i++) {System.out.println ("Thread 2" + i + "the second execution!" "); try {thread.sleep (m);} catch (Interruptedexception e) {e.printstacktrace ()}} } }

The results of the operation are as follows:

?

1 2 3 4 5 6 7 8 Thread 2 executes for the NO. 0 time! Thread 1 executes for the NO. 0 time! Thread 1 executes for the 1th time! Thread 2 executes for the 1th time! Thread 1 executes for the 2nd time!   Thread 2 executes for the 2nd time! Process finished with exit code 0

From the results above, it can be seen that the thread execution order cannot be guaranteed accurately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.