静态变量
/** * @author luxiangxing * @time 2017年5月7日 * @email xiangxingchina@163.com * @tel 15330078427 */ public class TwoThread implements Runnable { private int id; static Integer c ; private Object o; public TwoThread(int id, Object o) { this.id = id; this.o = o; } @Override public void run() { synchronized (o) { while (this.c < 1000) { // System.out.println(id+"> "+this.c); if (this.c % 2 == id) { System.out.println(id + " - " + c++); o.notify(); } else { try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { Object o = new Object(); TwoThread t1 = new TwoThread(1, o); TwoThread t2 = new TwoThread(0, o); new Thread(t1).start(); new Thread(t2).start(); } }
对象方式
/** * @author luxiangxing * @time 2017年5月7日 * @email xiangxingchina@163.com * @tel 15330078427 */ public class TwoThread implements Runnable { private int id; static Integer c ; private Object o; Num n ; public TwoThread(int id, Object o,Num n) { this.id = id; this.o = o; this.n = n; } @Override public void run() { synchronized (o) { while (this.n.i < 1000) { // System.out.println(id+"> "+this.c); if (this.n.i % 2 == id) { System.out.println(id + " - " + this.n.i); this.n.setI(this.n.i+1); o.notify(); } else { try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { Object o = new Object(); Num n = new Num(1); TwoThread t1 = new TwoThread(1, o,n); TwoThread t2 = new TwoThread(0, o,n); new Thread(t1).start(); new Thread(t2).start(); } } class Num{ int i ; public Num(int i) { this.i = i ; } public void setI(int i) { this.i = i; } }
相关推荐
松下plc 轮询 标准 程序 程式,基于松下plc做的485通讯和几台其他设备通信
S7-200SMART_modbusRTU轮询程序(读轮询+写优先)附使用说明
主要介绍了c# 实现轮询算法实例代码的相关资料,这里附有实例代码,具有一定的参考价值,需要的朋友可以参考下
单片机 程序架构 时间片轮询.
图片轮询查看
S7-200SMART PLC进行MODBUS通信轮询卡死时重新开启轮询的具体方法演示
init.c轮询注册事件init.c轮询注册事件init.c轮询注册事件init.c轮询注册事件
8通道输入,1通道输出,采取请求应答方式,对8通道输入进行仲裁,采用公平轮询机制;
Ajax轮询,Ajax长轮询和websocket.docx
本文实例为大家分享了Android实现轮询的方式,供大家参考,具体内容如下 1.通过rxjava实现(代码中使用了Lambda表达式) private static final int PERIOD = 10 * 1000; private static final int DELAY = 100; ...
本程序采用文件的形式,按照设定轮询时间对数据进行轮询显示
vb6 modbus 进行轮询读取 接收数据长度正确后再轮询 并将接收数据依次写入三个textbox中
zencart不跳转账号轮询
单片机中断方式与轮询方式 单片机中断方式与轮询方式
基于TIA PORTAL V15.0的MODBUS RTU轮询全局库FB,调用该FB之后,不需要再写大量的轮询程序。
stm32使用轮询方式来检测串口的接收数据,没有使用中断,适合于初学者
那么,什么是单片机中断,它与CPU的轮询有什么区别?在本文中,单片机开发工程师将对单片机中断和CPU轮询做个简单的介绍。 中断: 中断是一种硬件机制,在这种机制中,设备会通知CPU它需要引起注意。中断可以...
消息推送,轮询,消息通知
轮询采集指令ModbusPLC详细说明,版本V3.2
S7-200SMART通过循环移位实现MODBUS RTU轮询的具体方法示例