Wait示例分析
wait方法使"当前线程"进入阻塞(等待)状态.
示例分析:
public class TestWait {
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread("t1");
synchronized (t){ //main线程持有t对象的锁
System.err.println(Thread.currentThread().getName() + "... start");
t.start(); //t被启动后,t执行run里面的方法会阻塞,因为当前线程main持有t对象的锁
System.err.println(Thread.currentThread().getName() + "... in wait...");
t.wait(2);//当前main线程进入阻塞(等待)状态,并且线程main释放t的锁,然后main等待notify后重新去竞争t的锁然后继续执行.
//t对象的锁被释放后,线程t执行run方法的sync会获取到锁然后得到执行,执行完后notify该对象t上面等待的线程main(本例子只有main)
//主线程main被唤醒,然后重新竞争t对象的锁,得到锁后,继续执行end.
System.err.println(Thread.currentThread().getName() + "... end");
}
}
static class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
synchronized (this){
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(Thread.currentThread().getName() + "_exec.."+i);
}
System.err.println(Thread.currentThread().getName() + "_ notify....");
notify();//执行完后notify该对象t上面等待的线程,比如主线程
}
}
}
}
Wait示例分析的更多相关文章
- ngRx 官方示例分析 - 3. reducers
上一篇:ngRx 官方示例分析 - 2. Action 管理 这里我们讨论 reducer. 如果你注意的话,会看到在不同的 Action 定义文件中,导出的 Action 类型名称都是 Action ...
- ngRx 官方示例分析 - 2. Action 管理
我们从 Action 名称开始. 解决 Action 名称冲突问题 在 ngRx 中,不同的 Action 需要一个 Action Type 进行区分,一般来说,这个 Action Type 是一个字 ...
- ngRx 官方示例分析 - 1. 介绍
ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book API 来查询图书,并保存自己的精选书籍列表. 菜单有两 ...
- ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)
致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation kamtoa simulation学习与示例分析(一) 源码学习与分析是学习ROS,包 ...
- join示例分析
join示例分析 public class TestJoin { public static void main(String[] args) throws InterruptedException ...
- yield示例分析
yield示例分析 public class TestYield { private static final Object lock = new Object(); public static vo ...
- Sleep示例分析
sleep让"当前线程"由“运行状态”进入到“休眠(阻塞)状态”,sleep结束,线程重新被唤醒时,它会由“阻塞状态”变成“就绪状态”,从而等待cpu的调度执行. 示例分析: pu ...
- ivew数控件Tree自定义节点内容示例分析
ivew数控件Tree自定义节点内容示例分析 demo地址:https://run.iviewui.com/plcWlM4H <template> <Tree :data=" ...
- TensorFlow入门和示例分析
本文以TensorFlow源码中自带的手写数字识别Example为例,引出TensorFlow中的几个主要概念.并结合Example源码一步步分析该模型的实现过程. 一.什么是TensorFlow 在 ...
随机推荐
- [Usaco2007 Oct] Super Paintball超级弹珠
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 489 Solved: 384[Submit][Status][Discuss] Description ...
- 【BZOJ1492】货币兑换Cash(CDQ分治)
题意: 小 Y 最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A 纪 念券(以下简称 A 券)和 B 纪念券(以下简称 B 券).每个持有金券的顾客都有 一个自己的帐户.金券的数目可以是一 ...
- BZOJ1693: [Usaco2007 Demo]Asteroids
n<=500 *n的格子,给m<=10000个格子有人,一炮可以清掉一行或一列的人(莫名的爽!)求最少几炮干掉所有人. 经典二分图模型!行成点,列成点,一个点就连接一行一列,表示这一行或这 ...
- WordPress升级错误:class-wp-filesystem-direct.php on line 122
错误描述:WordPress在后台进行版本升级时,出错,之后进入前台或者后台,都无法访问进入,错误信息如下:Warning: copy(/home/xxx/public_html/wordpress/ ...
- @Aspect注解无效
Pointcut的execution配置正确的话,检查下,是否加了以下jar包 <!-- http://mvnrepository.com/artifact/org.aspectj/aspect ...
- css中高度比img多出4px的问题
一句话概括:为什么<a>标签比里面的img高度多出4px 的问题,主要还是由于 img是inline element, it's height is caculated different ...
- [Angular] Communicate Between Components Using Angular Dependency Injection
Allow more than one child component of the same type. Allow child components to be placed within the ...
- jquery 动态添加,降低input表单的方法
html代码例如以下 <html> <tr><button style="margin-left:10px" class="add_fiel ...
- onfocus事件,onblur事件;Focus()方法,Blur()方法
<1> <pre name="code" class="html"><!DOCTYPE html PUBLIC "-// ...
- 理解Android ANR的触发原理(转)
一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...