线程interrupt和wait

Posted mutong1228

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程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();
}
}
 

以上是关于线程interrupt和wait的主要内容,如果未能解决你的问题,请参考以下文章

Java——多线程高并发系列之wait()notify()notifyAll()interrupt()

Java——多线程高并发系列之wait()notify()notifyAll()interrupt()

Object的wait()和notify()以及notifyAll()和interrupt()方法

Object的wait()和notify()以及notifyAll()和interrupt()方法

Java 多线程基础interrupt()和线程终止方式

JVM线程状态,park, wait, sleep, interrupt, yeild 对比