Java multi-thread series-thread sleep in "basic articles" 07

Source: Internet
Author: User

1. sleep () introduces the definition of sleep () in Thread. java. Sleep () is used to sleep the current thread, that is, the current thread enters the sleep (blocking) state from the "running state ". Sleep () specifies the sleep time. The sleep time of a thread is later than or equal to the sleep time. When the thread is awakened again, the sleep time changes from blocking to ready ", so as to wait for cpu scheduling. 2. The sleep () Example below demonstrates the usage of sleep () through a simple example. Copy code 1 // SleepTest. java source code 2 class ThreadA extends Thread {3 public ThreadA (String name) {4 super (name); 5} 6 public synchronized void run () {7 try {8 for (int I = 0; I <10; I ++) {9 System. out. printf ("% s: % d \ n", this. getName (), I); 10 // when I can be divisible by 4, sleep for 100 milliseconds 11 if (I % 4 = 0) 12 Thread. sleep (100); 13} 14} catch (InterruptedException e) {15 e. printStackTrace (); 16} 17} 18} 19 20 public class SleepTest {21 public static void main (String [] args) {22 ThreadA t1 = new ThreadA ("t1"); 23 t1.start (); 24} 25} copy the code running result: copy the code t1: 0t1: 1t1: 2t1: 3t1: 4t1: 5t1: 6t1: 7t1: 8t1: 9, start thread t1 in main. After t1 is started, when the computing I in t1 can be fully divided by 4, t1 will sleep for 100 milliseconds through Thread. sleep (100. 3. sleep () and wait () are compared. We know that wait () is used to bring the current thread from the "running state" to the "waiting (blocking) state" at the same time, the synchronization lock is also released. Sleep () also enables the current thread to enter the sleep (blocking) state from the "running state ". However, wait () releases the synchronization lock of the object, while sleep () does not. The following example demonstrates that sleep () does not release the lock. Copy code 1 // SleepLockTest. java source code 2 public class SleepLockTest {3 4 private static Object obj = new Object (); 5 6 public static void main (String [] args) {7 ThreadA t1 = new ThreadA ("t1"); 8 ThreadA t2 = new ThreadA ("t2"); 9 t1.start (); 10 t2.start (); 11} 12 13 static class ThreadA extends Thread {14 public ThreadA (String name) {15 super (name); 16} 17 public void run () {18 // get the synchronization lock 19 synchroniz Of The obj object Ed (obj) {20 try {21 for (int I = 0; I <10; I ++) {22 System. out. printf ("% s: % d \ n", this. getName (), I); 23 // when I can be divisible by 4, sleep for 100 milliseconds 24 if (I % 4 = 0) 25 Thread. sleep (100); 26} 27} catch (InterruptedException e) {28 e. printStackTrace (); 29} 30} 31} 32} 33} copy the code run result: copy the code t1: 0t1: 1t1: 2t1: 3t1: 4t1: 5t1: 6t1: 7t1: 8t1: 9t2: 0t2: 1t2: 2t2: 3t2: 4t2: 5t2: 6t2: 7t2: 8t2: 9 copy code result Description: two threads t1 and t2 are started in the main thread. T1 and t2 reference the synchronization lock of the same object in run (), that is, synchronized (obj ). During t1 running, although it will call Thread. sleep (100), t2 will not obtain the cpu execution right. Because t1 does not release the "synchronization lock held by obj "! NOTE: If we comment out synchronized (obj) and execute this program again, t1 and t2 can switch between each other. The following is the source code after synchronized (obj) is annotated: Copy code 1 // SleepLockTest. java source code (comment out synchronized (obj) 2 public class SleepLockTest {3 4 private static Object obj = new Object (); 5 6 public static void main (String [] args) {7 ThreadA t1 = new ThreadA ("t1"); 8 ThreadA t2 = new ThreadA ("t2"); 9 t1.start (); 10 t2.start (); 11} 12 13 static class ThreadA extends Thread {14 public ThreadA (String name) {15 super (name); 16} 17 public void run () {18 // get the synchronization lock of the obj Object 19 // synchronized (obj) {20 try {21 for (int I = 0; I <10; I ++) {22 System. out. printf ("% s: % d \ n", this. getName (), I); 23 // when I can be divisible by 4, sleep for 100 milliseconds 24 if (I % 4 = 0) 25 Thread. sleep (100); 26} 27} catch (InterruptedException e) {28 e. printStackTrace (); 29} 30 //} 31} 32} 33} copy the code

Related Article

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.