zoukankan      html  css  js  c++  java
  • 三个线程之间的通信

    //使用while循环,当线程1执行结束后唤醒其他所有线程并释放锁,线程1和线程2和线程3争抢锁,需要确保线程2争取到锁,假设线程3和线程1抢到锁则进入waiting状态。依次类推。。。。。
    public
    class Demo { static int flag = 1; static Object ob = new Object(); public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { while (true) { synchronized (ob) { while (flag != 1) { try { ob.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } method1(); flag = 2; ob.notifyAll(); } } } }).start(); new Thread() { @Override public void run() { while (true) { synchronized (ob) { while (flag != 2) { try { ob.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } method2(); flag = 3; ob.notifyAll(); } } } }.start(); new Thread() { @Override public void run() { while (true) { synchronized (ob) { while (flag != 3) { try { ob.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } method3(); flag = 1; ob.notifyAll(); } } } }.start(); } public static void method1(){ System.out.print("a"); System.out.print("b"); System.out.print("c"); System.out.print("d"); System.out.println(" "); } public static void method2(){ System.out.print("A"); System.out.print("B"); System.out.print("C"); System.out.print("D"); System.out.println(" "); } public static void method3(){ System.out.print("1"); System.out.print("2"); System.out.print("3"); System.out.print("4"); System.out.println(" "); } }
  • 相关阅读:
    ThinkPHP Model+数据库的切换使用
    关于SSD安装系统的一些设置(PE安装win 7)
    PHP实现文件下载:header
    Thinkphp 使用PHPExcel导入,栗子
    Ueditor 的使用(这里以php+ci为例)
    js获取鼠标选中的文字内容
    WNMP 下 Nginx 配置 (使用了phpfind一键安装环境)
    javascript 实现 trim
    javascript 获取 CSS 样式表属性
    javascript 删除节点问题
  • 原文地址:https://www.cnblogs.com/chenfx/p/13365532.html
Copyright © 2011-2022 走看看