其他分享
首页 > 其他分享> > 02.线程的等待与中断

02.线程的等待与中断

作者:互联网

public class NotifyDemo {
    //创建资源
    private static volatile Object reA = new Object();
    private static volatile Object reB = new Object();
    //创建线程
    public static void main(String[] args) throws InterruptedException{
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获取reA共享资源的监视器锁
                    synchronized (reA){
                        System.out.println("threadA get reA lock");
                        //获取reB共享资源的监视器锁
                        synchronized (reB){
                            System.out.println("threadA get reB lock");
                            System.out.println("threadA release resourceA lock");
                            //当线程调用共享对象的wait()方法时,当前线程只会释放当前共享对象的锁
                            reA.wait();
                        }
                    }
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    //获取reA共享资源的监视器锁
                    synchronized (reA){
                        System.out.println("threadB get reA lock");
                        //获取reB共享资源的监视器锁
                        synchronized (reB){
                            System.out.println("threadB get reB lock");
                            System.out.println("threadB release resourceA lock");
                            reA.wait();
                        }
                    }
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });
        threadA.start();
        threadB.start();
        //等待两个线程结束
        threadA.join();
        threadB.join();

        //threadA get reA lock
        //threadA get reB lock
        //threadA release resourceA lock
        //threadB get reA lock
    }
}
/**
 *  threadA 调用共享对象 obj 的 wait()方法后阻塞挂起了自己,然后 主线程在休眠 ls 后中断了 threadA 线程,
 *  中断后 threadA 在 obj.wait()处抛出 java.lang. InterruptedException 异常而返回并终止。
 */
public class NotifyDemo2 {
    //创建资源
    private static Object obj = new Object();
    //创建线程
    public static void main(String[] args) throws InterruptedException{
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获取reA共享资源的监视器锁
                    synchronized (obj){
                        System.out.println("begin");
                        obj.wait();
                    }
                    System.out.println("begin");
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        });

        threadA.start();
        Thread.sleep(1000);
        System.out.println("begin interrupt");
        threadA.interrupt();//中断threadA线程
        System.out.println("end interrupt");
        //begin
        //begin interrupt
        //end interrupt
        //java.lang.InterruptedException
        //  at java.base/java.lang.Object.wait(Native Method)
        //  at java.base/java.lang.Object.wait(Object.java:516)
        //  at com.fly.NotifyDemo2$1.run(NotifyDemo2.java:15)
        //  at java.base/java.lang.Thread.run(Thread.java:844)
    }
}

标签:02,java,threadA,中断,lock,System,线程,reA,out
来源: https://www.cnblogs.com/fly-book/p/11358423.html