public class T {

    private static int a =1;//1代表线程1 2线程2

    public static void main(String[] args) {

        final T t = new T();

        new Thread(new Runnable() {

            @Override
public void run() {
synchronized (t) {
for(int i=1;i<=10;i++){
if(i==6){
try {
a=2;//切换线程2
t.wait();//线程1等待,并释放了对象的锁
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}).start();; new Thread(new Runnable(){ @Override
public void run() {
synchronized (t) {
if(a!=2){//没有轮到,进入等待
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("hello");
t.notifyAll(); } } }).start(); }
}

结果如下:

Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-1:hello
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
Thread-0:10

最新文章

  1. webapi集成owin使用Oauth认证时能获取accee_token仍无法登录的解决办法
  2. Windows下MongoDB环境搭建
  3. map初始化定时器
  4. 谈谈文件增量同步算法:RSYNC和CDC
  5. reason: &#39;unable to dequeue a cell with identifier Cell
  6. 阿里开源Mysql分布式中间件:Cobar
  7. PHP文件上传处理
  8. Kubernetes DNS 简介
  9. NodeJS在线聊天室(NodeJS &amp; SocketIO &amp; Express &amp; EJS &amp; MongoDB &amp; Gulp)
  10. ASP.NET Core 2.0 自定义 _ViewStart 和 _ViewImports 的目录位置
  11. 【HTTP原理】TCP/IP三次握手和四次挥手
  12. Typescript 常见的几种函数重载方法详解与应用示例
  13. 学习笔记5—Python 将多维数据转为一维数组 (总结)
  14. kmp模式串匹配
  15. 关于requests.exceptions.SSLError: HTTPSConnectionPool
  16. Atitit 跨平台的系统截图解决方案
  17. uva 10983 Buy one, get the rest free 二分判定层次图
  18. WebSocket(二)-WebSocket、Socket、TCP、HTTP区别
  19. Python中参数多个值的表示法
  20. Spring mvc中@RequestMapping 6个基本用法小结

热门文章

  1. BZOJ3526 [Poi2014]Card 【线段树】
  2. HDU 2700
  3. 数组分组chunk的一种写法
  4. SRM13 T3 花六游鸟小(结论题)
  5. 什么是static?什么是final?
  6. mysql concat update中拼接字符串
  7. Sqlserver中如何创建链接服务器
  8. 浏览器报错:unexpected end of input 解决方法
  9. vijos 1081 野生动物园 函数式线段树
  10. LightOJ 1375 - LCM Extreme 莫比乌斯反演或欧拉扩展