Java basic tutorial thread sleep Java multithreading tutorial

  • 2020-04-01 02:43:51
  • OfStack

The contents covered in this chapter include:
1. The sleep () is introduced
2. The sleep () example
3. Sleep () versus wait()

1. The sleep () is introduced
Sleep () is defined in thread.java.
Sleep () allows the current thread to sleep, which means that the current thread will go from "run" to "sleep". Sleep () specifies a sleep time that is greater than/equal to the sleep time of the thread. When the thread is reawakened, it changes from "blocked" to "ready" and waits for the CPU's schedule to execute.


2. The sleep () example
Here's a simple example of how to use sleep().


 //SleepTest. Java source code
 class ThreadA extends Thread{
     public ThreadA(String name){ 
         super(name); 
     } 
     public synchronized void run() { 
         try {
             for(int i=0; i <10; i++){ 
                 System.out.printf("%s: %dn", this.getName(), i); 
                //Sleep for 100 milliseconds when I is divisible by 4
                if (i%4 == 0)
                    Thread.sleep(100);
            } 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
} 
public class SleepTest{ 
    public static void main(String[] args){ 
        ThreadA t1 = new ThreadA("t1"); 
        t1.start(); 
    } 
} 

Operation results:


t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9

Results:
The program is simple, starting thread t1 in the main thread. After t1 starts, when the calculated I in t1 is divisible by 4, t1 will sleep for 100 milliseconds through thread.sleep (100).

Sleep () versus wait()
We know that wait() releases the synchronization lock as it moves the current thread from "run" to "wait". Sleep (), on the other hand, allows the current thread to go from "run" to "sleep".
However, wait() releases the synchronized lock for the object, while sleep() does not.
The following example demonstrates that sleep() does not release locks.


 //SleepLockTest. Java source code
 public class SleepLockTest{ 

     private static Object obj = new Object();

     public static void main(String[] args){ 
         ThreadA t1 = new ThreadA("t1"); 
         ThreadA t2 = new ThreadA("t2"); 
         t1.start(); 
        t2.start();
    } 
    static class ThreadA extends Thread{
        public ThreadA(String name){ 
            super(name); 
        } 
        public void run(){ 
            //Gets the synchronization lock for the obj object
            synchronized (obj) {
                try {
                    for(int i=0; i <10; i++){ 
                        System.out.printf("%s: %dn", this.getName(), i); 
                        //Sleep for 100 milliseconds when I is divisible by 4
                        if (i%4 == 0)
                            Thread.sleep(100);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } 
    } 
} 

Operation results:


t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t1: 5
t1: 6
t1: 7
t1: 8
t1: 9
t2: 0
t2: 1
t2: 2
t2: 3
t2: 4
t2: 5
t2: 6
t2: 7
t2: 8
t2: 9

Results:
Two threads, t1 and t2, are started in the main thread. T1 and t2 refer to synchronized(obj), a synchronized lock on the same object, in run(). While t1 is running, it calls thread.sleep (100); However, t2 does not get CPU execution. Because, t1 does not release the synchronization lock held by obj!
Note that t1 and t2 can be switched if we execute the program again after commenting out synchronized (obj). The following is the source code after commented synchronized(obj) :


 //Java source (comment out synchronized(obj))
 public class SleepLockTest{ 

     private static Object obj = new Object();

     public static void main(String[] args){ 
         ThreadA t1 = new ThreadA("t1"); 
         ThreadA t2 = new ThreadA("t2"); 
         t1.start(); 
        t2.start();
    } 
    static class ThreadA extends Thread{
        public ThreadA(String name){ 
            super(name); 
        } 
        public void run(){ 
            //Gets the synchronization lock for the obj object
//            synchronized (obj) {
                try {
                    for(int i=0; i <10; i++){ 
                        System.out.printf("%s: %dn", this.getName(), i); 
                        //Sleep for 100 milliseconds when I is divisible by 4
                        if (i%4 == 0)
                            Thread.sleep(100);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//            }
        } 
    } 
} 


Related articles: