java中的wait(time), notify(), notifyall?是怎么回事?内核层面的实现也是如此

结论:

wait(time), notify(), notifyall?

一个线程拿到一个对象上的锁,才能调用这个对象的wait(time),notify,notifyall!!!否则会抛异常。
线程没有休眠和苏醒的概念,只有从调度队列排除和加入的概念。前者两个概念要误导人!!
调用wait(time)会做3件事:
1、使当前线程加入当这个对象的等待线程集合,这个是为什么线程要拿到这个对象的锁才能够调用wait。(加入等待set)
2、同时会使这个线程释放他拿到这个对象上的锁。(还锁)
3、从调度队列中排除。

调用wait的线程重新加入调用队列,只有当一下四件事情发生时:
1、有其他线程调用这个对象的notifyall,
2、有其他线程调用notify,且这个线程又刚好随机被notify选中
3、有其他线程调用这个对象intercept方法,将线程的中断标志位置位(当线程投入使用时, wait方法要抛一个中断异常)
4、时间超时了。

代码验证:

控制台输出:

sub thread1
sub thread2
sub thread3
main thread 


代码如下:


public class ThreadTest {
    private  Object o = new Object();
    public ThreadTest() {
    }

    public void test() throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("sub thread1");
                }catch (Exception e){

                }
            }
        };

        final Thread thread = new Thread(runnable);
        thread.start();
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                try {
                    thread.join();
                    System.out.println("sub thread2");
                }catch (Exception e){

                }
            }
        };
        final Thread main = Thread.currentThread();
        Thread thread1 = new Thread(runnable1);
        thread1.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 1000000000; i++){
                    if (i == 1000000000){
                        System.out.println("sub thread3");
                    }
                }
                try {
                    main.interrupt();//这里写main.interupt();想过一样
                }catch (Exception e){

                }


            }
        }).start();
        thread1.join();

        try{
            synchronized (o){
                System.out.println("main wait");
               o.wait();
            }
        }catch (Exception e){
            System.out.println("main thread excet");
        }



    }


    public static void main(String[] args) throws Exception{
        new ThreadTest().test();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值