• 线程interrupt和wait


    public class InterruptDemo {
        public static void main(String[] args) throws InterruptedException{
            Thread t1 = new Thread(){
                public void run(){
                    while (true){
                        System.out.println("线程执行");
                        if(Thread.currentThread().isInterrupted()){
                            System.out.println("线程被中断执行");
                            break;
                        }
                        try{
                            Thread.sleep(3000);
    
                        }catch (InterruptedException e){
                            System.out.println("线程出现异常");
                            //如果抛异常,会清除中断标记  再次设置中断
                            Thread.currentThread().interrupt();
                        }
                        //使线程从执行状态到就绪状态
                        Thread.yield();
                    }
                }
            };
            t1.start();
            Thread.sleep(3000);
            /*
            保证线程可见性
            private volatile Interruptible blocker;
            private final Object blockerLock = new Object();
    
            public void interrupt() {
            if (this != Thread.currentThread())
                checkAccess();
    
            //使用同步代码块
            synchronized (blockerLock) {
                Interruptible b = blocker;
                if (b != null) {
                    interrupt0();           // Just to set the interrupt flag
                    b.interrupt(this);
                    return;
                }
            }
            //private native void interrupt0();
            interrupt0();
        }
             */
            t1.interrupt();
        }
    }
    public class WaitDemo {
    private final static Object object = new Object();

    Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
    synchronized(object){
    System.out.println(Thread.currentThread().getName() + "等待的时间开始:" + System.currentTimeMillis());
    System.out.println("执行1:" + Thread.currentThread().getName());
    try{
    //wait方法会释放目标对象的锁
    object.wait();
    }catch (InterruptedException e){
    System.out.println("异常1:" + e.getMessage());
    }
    System.out.println(Thread.currentThread().getName() + "等待的时间结束:" + System.currentTimeMillis());
    }
    }
    });
    Thread t2 = new Thread(new Runnable() {
    @Override
    public void run() {
    synchronized(object){
    System.out.println(Thread.currentThread().getName() + "等待的时间开始:" + System.currentTimeMillis());
    System.out.println("执行2:" + Thread.currentThread().getName());
    //唤醒所有等待的线程
    //object.notifyAll();
    //唤醒等待的线程 随机唤醒一个
    object.notify();
    try{
    Thread.sleep(2000);
    }catch (InterruptedException e){
    System.out.println("异常2:" + e.getMessage());
    }
    System.out.println(Thread.currentThread().getName() + "等待的时间结束:" + System.currentTimeMillis());
    }
    }
    });
    public static void main(String[] args){
    WaitDemo wd = new WaitDemo();
    //两个线程执行的顺序是随机的,原因是多个线程会进行指令重排
    wd.t1.start();
    wd.t2.start();
    }
    }
     
  • 相关阅读:
    SQL Server 2016,2014 “无法找到数据库引擎启动句柄”
    SCCM Collection 集合获取计算机最后启动时间
    在Office 365 添加就地保留用户邮箱
    SQL Server数据库log shipping 灾备(Part2 )
    SQL Server数据库log shipping 灾备(Part1 )
    领域驱动设计案例之实现业务3
    领域驱动设计案例之业务实现2
    领域驱动设计案例之业务实现1
    领域驱动设计案例之仓储顶层实现
    领域驱动设计案例之领域层实体与聚合根实现
  • 原文地址:https://www.cnblogs.com/mutong1228/p/10559199.html
Copyright © 2020-2023  润新知