码迷,mamicode.com
首页 > 编程语言 > 详细

线程interrupt和wait

时间:2019-03-19 16:31:47      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:代码   wait   代码块   check   同步代码块   throws   new t   row   cat   

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();
}
}
 

线程interrupt和wait

标签:代码   wait   代码块   check   同步代码块   throws   new t   row   cat   

原文地址:https://www.cnblogs.com/mutong1228/p/10559199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!