Home  >  Article  >  Java  >  Introduction to waiting and waking up of threads in Java (code)

Introduction to waiting and waking up of threads in Java (code)

不言
不言forward
2018-10-08 15:48:372898browse

This article brings you an introduction (code) about waiting and waking up threads in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

class ThreadA extends Thread{

    public ThreadA(String name) {
        super(name);
    }

    public void run() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName()+" call notify()");
            notify();
        }
    }
}
public class WaitTest {
    public static void main(String[] args) {
        ThreadA t1 = new ThreadA("t1");
        synchronized(t1) {
            try {
                // 启动“线程t1”
                System.out.println(Thread.currentThread().getName()+" start t1");
                t1.start();

                // 主线程等待t1通过notify()唤醒。
                System.out.println(Thread.currentThread().getName()+" wait()");
                t1.wait();

                System.out.println(Thread.currentThread().getName()+" continue");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Output result: main start t1 -> main wait() -> t1 call notify() -> main continue

In fact, when t1.start() is called, t1 is in the ready state, but in the main method, t1 is locked by the main thread. When t1.wait(), letting the current thread wait actually makes the main thread wait and then releases it. After locking t1, the t1 thread executes, prints t1 call notify(), then wakes up the main thread, and finally ends;

Let’s talk about the difference between wait() and sleep(). What they have in common is to let the thread Sleep, but wait() will release the object synchronization lock, but sleep() will not; the following code will not run t2 until t1 ends; this can be confirmed;

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(){ 
            synchronized (obj) {
                try {
                    for(int i=0; i <10; i++){ 
                        System.out.printf("%s: %d\n", this.getName(), i); 
                        // i能被4整除时,休眠100毫秒
                        if (i%4 == 0)
                            Thread.sleep(100);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } 
    } 
}

The above is the detailed content of Introduction to waiting and waking up of threads in Java (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete