多线程编程 使线程具有有序性
来源:互联网 发布:声音剪辑合成软件 编辑:程序博客网 时间:2024/06/05 00:47
package com.multithreading.pc73;/** * Created by nanzhou on 2017/6/30. */public class MyThread extends Thread { private Object lock; private String showCar; private int showNumPosition; private int printCount; volatile private static int addNumber = 1; public MyThread(Object lock, String showCar, int showNumPosition) { super(); this.lock = lock; this.showCar = showCar; this.showNumPosition = showNumPosition; } @Override public void run() { try { synchronized (lock) { while (true) { if (addNumber % 4 == showNumPosition) { System.out.println("ThreadName " + Thread.currentThread().getName() + " runCount " + addNumber + " " + showCar); lock.notifyAll(); addNumber++; printCount++; if (printCount == 10) { break; } } else { lock.wait(); } } } } catch (InterruptedException e) { e.printStackTrace(); } }}
package com.multithreading.pc73;/** * Created by nanzhou on 2017/6/30. */public class Run { public static void main(String[] args) { Object lock = new Object(); MyThread a = new MyThread(lock, "A", 1); MyThread b = new MyThread(lock, "B", 2); MyThread c = new MyThread(lock, "C", 3); MyThread d = new MyThread(lock, "D", 0); a.start(); b.start(); c.start(); d.start(); }}
阅读全文
0 0
- 多线程编程 使线程具有有序性
- JAVA多线程——线程安全之原子性,有序性和可见性
- 有序性
- 【多线程】多线程编程:线程同步
- 【多线程】多线程编程:线程通信
- 多线程编程---线程死锁
- 多线程编程:线程同步
- 保证线程的有序性,使用jion方法
- 多线程编程与线程同步
- 多线程编程之线程取消
- C++多线程编程(3)线程
- 多线程编程5 线程同步
- 多线程编程之线程通信
- Java多线程编程--初识线程
- Linux 多线程编程 线程属性
- 多线程编程--- 线程池 threadpool
- 多线程学习之多线程编程
- C++多线程编程(线程类)
- Linux下 XordDos(BillGates)木马查杀记录
- UTF-8、en_US.UTF-8和zh_CN.UTF-8的区别
- 从C++函数返回多个值
- 四元组相加获得target
- 高性能序列化protostuff
- 多线程编程 使线程具有有序性
- Linux文件查找命令
- 使用spring aop管理事务
- linux+jenkins+gradle+gitlab自动化打包发布andriod APP
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; che
- MySQL入门---登陆、退出、修改提示符、常用规范
- ajax中的timeout
- java Thread interrupt方法
- WAF绕过参考资料