/**
* 测试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

重学JAVA基础(七):线程的wait、notify、notifyAll、sleep的更多相关文章

  1. 重学JAVA基础(四):线程的创建与执行

    1.继承Thread public class TestThread extends Thread{ public void run(){ System.out.println(Thread.curr ...

  2. 重学JAVA基础(八):锁的基本知识

    1.线程状态 如上图,当我们新建一个线程,并start后,其实不一定会马上执行,因为只有操作系统调度了我们的线程,才能真正进行执行,而操作系统也随时可以运行其他线程,这时线程又回到可运行状态.这个过程 ...

  3. 重学JAVA基础(六):多线程的同步

    1.synchronized关键字 /** * 同步关键字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */ public class SyncT ...

  4. 重学JAVA基础(五):面向对象

    1.封装 import java.util.Date; public class Human { protected String name; protected BirthDay birthDay; ...

  5. 重学JAVA基础(二):Java反射

        看一下百度的解释:       JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息     ...

  6. 重学JAVA基础(一):PATH和CLASSPATH

    我想大多数Java初学者都会遇到的问题,那就是怎么配置环境,执行java -jar xxx.jar  都会报NoClassDefFindError,我在最开始学习的时候,也遇到了这些问题. 1.PAT ...

  7. 重学JAVA基础(三):动态代理

    1.接口 public interface Hello { public void sayHello(); } 2.实例类 public class Hello2 { public void sayH ...

  8. 重学 Java 设计模式:实战享元模式「基于Redis秒杀,提供活动与库存信息查询场景」

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 程序员‍‍的上下文是什么? 很多时候一大部分编程开发的人员都只是关注于功能的实现,只 ...

  9. 12天,这本《重学Java设计模式》PDF书籍下载量9k,新增粉丝1400人,Github上全球推荐榜!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言

随机推荐

  1. No module named yum错误的解决办法

    今天用yum安装软件的时候出现如下错误: There was a problem importing one of the Python modules required to run yum. Th ...

  2. 关于hql一些不常见但好用的技巧(个人总结)

    最近一直在用spring-data-jpa这个东西,感觉方法上注解hql语句已经是很常用的方法了, 有一些关于hql的经验分享一下: 一.hql的join hql的优势就是直接的关联关系嘛,但是通过h ...

  3. Xshell快捷键

    Ctrl + L     清除工作区的内容

  4. 实战录 | 基于openflow协议的抓包分析

    <实战录>导语 云端卫士<实战录>栏目定期会向粉丝朋友们分享一些在开发运维中的经验和技巧,希望对于关注我们的朋友有所裨益.本期分享人为云端卫士安全SDN工程师宋飞虎,将带来基于 ...

  5. 20145225 《Java程序设计》 第3周学习总结

    20145225<Java程序设计> 第3周学习总结 教材学习内容总结 4.1类与对象 相当于设计图纸,用"new"创建的对象,就是依据设计图做成的成品 . 例(定义C ...

  6. c# 通过关键字查询

    1:首先需要在前端显示界面View视图中添加查询按钮: <div> <div>@Html.NopLabelFor(model => model.IndividualNam ...

  7. 斯坦福第十八课:应用实例:图片文字识别(Application Example: Photo OCR)

    18.1  问题描述和流程图 18.2  滑动窗口 18.3  获取大量数据和人工数据 18.4  上限分析:哪部分管道的接下去做 18.1  问题描述和流程图

  8. android中include 的使用讲解

    include的作用就是重复使用同一段代码,提高代码的重用性.具体说就是,通过include 在 某布局 a.xml 中引用 B.xml布局文件,这个b.xml可同时被多个布局同时使用,所以达到了同一 ...

  9. vpn与局域网冲突解决方案

    打开vpn后,所有通过网卡发出去的包都会走vpn,而不会走局域网,所以局域网无法访问,vpn为本机指定路由,让网卡把包发给vpn线路(比如10网段).如果为本机指定到达局域网的路由,访问局域网的包就知 ...

  10. Dynamic CRM 2013学习笔记(三十)Linq使用报错 A proxy type with the name account has been defined by another assembly

    在CRM中使用linq时,有时会报这个错误: A proxy type with the name account has been defined by another assembly. Curr ...