重学JAVA基础(七):线程的wait、notify、notifyAll、sleep

复制代码
/**
 * 测试thread的wait  notify  notifyAll  sleep  Interrupted
 * @author tomsnail
 * @date 2015年4月20日 下午3:20:44
 */
public class Test1 {
    
    /**
     * 对象锁
     * @author tomsnail
     * @date 2015年4月20日 下午3:14:13
     */
    private static final Object lockObject = new Object();
    
    /**
     * 等待线程
     * @author tomsnail
     * @date 2015年4月20日 下午3:14:22
     */
    static class Thread1 implements Runnable{

        @Override
        public void run() {
            synchronized (lockObject) {
                try {
                    System.out.println(Thread.currentThread().getName()+"wait start");
                    lockObject.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"wait end");
            }
        }
        
    }
    
    /**
     * 唤醒线程
     * @author tomsnail
     * @date 2015年4月20日 下午3:14:36
     */
    static class Thread2 implements Runnable{

        @Override
        public void run() {
            synchronized (lockObject) {
                lockObject.notify();
                System.out.println(Thread.currentThread().getName()+"notify");
            }
        }
    }
    
    /**
     * 唤醒所有线程
     * @author tomsnail
     * @date 2015年4月20日 下午3:14:51
     */
    static class Thread3 implements Runnable{

        @Override
        public void run() {
            synchronized (lockObject) {
                lockObject.notifyAll();
                System.out.println(Thread.currentThread().getName()+"notifyAll");
            }
        }
    }
    
    /**
     * 睡眠线程
     * @author tomsnail
     * @date 2015年4月20日 下午3:20:30
     */
    static class Thread4 implements Runnable{

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName()+"sleep");
                Thread.currentThread().sleep(20000);
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName()+"Interrupted");
            }
            
        }
        
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new Thread1());
        Thread t3 = new Thread(new Thread1());
        Thread t4 = new Thread(new Thread1());
        Thread t2 = new Thread(new Thread2());
        Thread t5 = new Thread(new Thread3());
        //3个等待线程运行
        t1.start();
        t3.start();
        t4.start();
        try {
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //唤醒线程运行
        t2.start();
        try {
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //唤醒所有线程运行
        t5.start();
        //睡眠线程
        Thread t6 = new Thread(new Thread4());
        t6.start();    
        try {
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //睡眠线程中断
        t6.interrupt();
    }

}
复制代码

结果

复制代码
Thread-0wait start
Thread-2wait start
Thread-1wait start
Thread-3notify
Thread-0wait end
Thread-4notifyAll
Thread-1wait end
Thread-2wait end
Thread-5sleep
Thread-5Interrupted
复制代码

 

posted @   TomSnail  阅读(312)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
· 重磅发布!DeepSeek 微调秘籍揭秘,一键解锁升级版全家桶,AI 玩家必备神器!
点击右上角即可分享
微信分享提示