java concurrency: Preliminary sleep method

sleep与wait

Thread method sleep is such that the current thread from the running state to the blocking state. But it will not release the lock object.
Object method wait method is, its role is to make the thread that currently owns the object lock state from running state becomes blocked,
it will release locks

sleep test

package com.java.javabase.thread.sleep;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SleepTest {
    private static int coutsize = 10;
    private static Object obj = new Object();

    public static void main(String[] args) {
        Thread t1 =new ThreadOne("t1");
        Thread t2 =new ThreadOne("t2");
        t1.start();
        t2.start();
    }

    static class ThreadOne extends Thread {
        public ThreadOne(String name){
            super(name);
        }
        @Override
        public void run() {
            synchronized (obj){
                int i = 0;
                try {
                    while (i < coutsize) {

                        log.info("current thread is : {} sleep {} times ",Thread.currentThread().getName(),i++);

                        Thread.sleep(300);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

Explanation

For two threads have the same Object object, when a thread holds the lock object, although multiple times Thread, thread blocked from running state to state, but it will not release the lock object,
other threads can only wait .

Test Results

2019-07-29 16:49:22,156   [t1] INFO  SleepTest  - current thread is : t1 sleep 0 times 
2019-07-29 16:49:22,469   [t1] INFO  SleepTest  - current thread is : t1 sleep 1 times 
2019-07-29 16:49:22,782   [t1] INFO  SleepTest  - current thread is : t1 sleep 2 times 
2019-07-29 16:49:23,094   [t1] INFO  SleepTest  - current thread is : t1 sleep 3 times 
2019-07-29 16:49:23,407   [t1] INFO  SleepTest  - current thread is : t1 sleep 4 times 
2019-07-29 16:49:23,720   [t1] INFO  SleepTest  - current thread is : t1 sleep 5 times 
2019-07-29 16:49:24,032   [t1] INFO  SleepTest  - current thread is : t1 sleep 6 times 
2019-07-29 16:49:24,345   [t1] INFO  SleepTest  - current thread is : t1 sleep 7 times 
2019-07-29 16:49:24,658   [t1] INFO  SleepTest  - current thread is : t1 sleep 8 times 
2019-07-29 16:49:24,970   [t1] INFO  SleepTest  - current thread is : t1 sleep 9 times 
2019-07-29 16:49:25,283   [t2] INFO  SleepTest  - current thread is : t2 sleep 0 times 
2019-07-29 16:49:25,596   [t2] INFO  SleepTest  - current thread is : t2 sleep 1 times 
2019-07-29 16:49:25,909   [t2] INFO  SleepTest  - current thread is : t2 sleep 2 times 
2019-07-29 16:49:26,221   [t2] INFO  SleepTest  - current thread is : t2 sleep 3 times 
2019-07-29 16:49:26,534   [t2] INFO  SleepTest  - current thread is : t2 sleep 4 times 
2019-07-29 16:49:26,847   [t2] INFO  SleepTest  - current thread is : t2 sleep 5 times 
2019-07-29 16:49:27,159   [t2] INFO  SleepTest  - current thread is : t2 sleep 6 times 
2019-07-29 16:49:27,472   [t2] INFO  SleepTest  - current thread is : t2 sleep 7 times 
2019-07-29 16:49:27,785   [t2] INFO  SleepTest  - current thread is : t2 sleep 8 times 
2019-07-29 16:49:28,098   [t2] INFO  SleepTest  - current thread is : t2 sleep 9 times 

Guess you like

Origin www.cnblogs.com/JuncaiF/p/11265945.html