高并发基础之线程通信wait/notify/notifyAll(四)

在多线程中通信我们可以使用wait/notify/notifyAll

这里顺带提一下sleep和wait的区别 

sleep:线程阻塞不释放锁

wait:线程阻塞释放锁


wait和notify和notifyAll是属于Object的方法所以所有类都可以调用到

notify唤醒阻塞中的线程,但是并不释放锁,需要等待线程执行完毕且释放锁才能由其他线程争夺锁

notifyAll和notify的区别在于notify唤醒是只有随机一个线程执行完其他的任然保持阻塞状态,而notifyAll能保障所有线程都会唤醒执行完

notify效果

public class NotifyTest {  
    private  Integer flag = new Integer(1);  
    class NotifyThread extends Thread{  
        public NotifyThread(String name) {  
            super(name);  
        }  
        public void run() { 
        	synchronized (flag) {
        		try {  
                    sleep(3000);//推迟3秒钟通知  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                    flag.notify();  
        		} 
			}
    };  
    class WaitThread extends Thread {  
        public WaitThread(String name) {  
            super(name);  
        }  
  
        public void run() {  
        	synchronized (flag) {
        		long waitTime = System.currentTimeMillis();  
                try {  
                    flag.wait();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                System.out.println("wait time :"+waitTime);  
			}
        }  
    }  
  
    public static void main(String[] args) throws InterruptedException {  
        System.out.println("Main Thread Run!");  
        NotifyTest test = new NotifyTest();  
        NotifyThread notifyThread =test.new NotifyThread("notify01");  
        WaitThread waitThread01 = test.new WaitThread("waiter01");  
        WaitThread waitThread02 = test.new WaitThread("waiter02");  
        WaitThread waitThread03 = test.new WaitThread("waiter03");  
        
        waitThread01.start();  
        waitThread02.start();  
        waitThread03.start();  
        notifyThread.start();  
    }  
  
} 

notifyAll效果

public class NotifyTest {  
    private  Integer flag = new Integer(1);  
    class NotifyThread extends Thread{  
        public NotifyThread(String name) {  
            super(name);  
        }  
        public void run() { 
        	synchronized (flag) {
        		try {  
                    sleep(3000);//推迟3秒钟通知  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                    flag.notifyAll();  
        		} 
			}
    };  
    class WaitThread extends Thread {  
        public WaitThread(String name) {  
            super(name);  
        }  
  
        public void run() {  
        	synchronized (flag) {
        		long waitTime = System.currentTimeMillis();  
                try {  
                    flag.wait();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                System.out.println("wait time :"+waitTime);  
			}
        }  
    }  
  
    public static void main(String[] args) throws InterruptedException {  
        System.out.println("Main Thread Run!");  
        NotifyTest test = new NotifyTest();  
        NotifyThread notifyThread =test.new NotifyThread("notify01");  
        WaitThread waitThread01 = test.new WaitThread("waiter01");  
        WaitThread waitThread02 = test.new WaitThread("waiter02");  
        WaitThread waitThread03 = test.new WaitThread("waiter03");  
        
        waitThread01.start();  
        waitThread02.start();  
        waitThread03.start();  
        notifyThread.start();  
    }  
  
} 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值