同步示例
来源:互联网 发布:织梦cms邀请码 编辑:程序博客网 时间:2024/05/22 11:41
package mypack;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/* * 一个是将蜡涂到car上,一个是将抛光它 * 抛光任务在涂蜡任务完成之前,是不能执行其他工作的 * 涂蜡任务在涂另一层蜡之前,必须等待抛光任务完成*/class Car{private boolean waxed=false;public synchronized void waxOn() throws InterruptedException{while(waxed)wait();TimeUnit.MILLISECONDS.sleep(200);waxed=true;System.out.println("Wax on");notifyAll();}public synchronized void waxOff() throws InterruptedException{while(!waxed)wait();TimeUnit.MILLISECONDS.sleep(200);waxed=false;System.out.println("wax off");notifyAll();}}class WaxOn implements Runnable{private Car car; public WaxOn(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry {while(!Thread.interrupted()){ car.waxOn();}} catch (InterruptedException e) {// TODO Auto-generated catch blockthrow new RuntimeException("Interrupt Wax on");}}}class WaxOff implements Runnable{private Car car;public WaxOff(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry {while(!Thread.interrupted())car.waxOff();} catch (InterruptedException e) {// TODO Auto-generated catch blockthrow new RuntimeException("Interrupt Wax Off");}}}public class WaxOMatic {public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stub ExecutorService exec = Executors.newCachedThreadPool(); Car car = new Car(); WaxOn waxOn = new WaxOn(car); WaxOff waxOff = new WaxOff(car); exec.execute(waxOn); exec.execute(waxOff); TimeUnit.SECONDS.sleep(5); exec.shutdownNow();}}
package mypack;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;import javax.swing.plaf.synth.SynthSpinnerUI;/* * 一个是将蜡涂到car上,一个是将抛光它 * 抛光任务在涂蜡任务完成之前,是不能执行其他工作的 * 涂蜡任务在涂另一层蜡之前,必须等待抛光任务完成*/class Car{private ReentrantLock lock=new ReentrantLock();private Condition condition=lock.newCondition();private boolean waxed=false;public void waxOn() throws InterruptedException{try{lock.lock();while(waxed) condition.await();TimeUnit.MILLISECONDS.sleep(200);System.out.println("WaxOn");waxed=true;condition.signalAll();} finally{lock.unlock();}}public void waxOff() throws InterruptedException{try{lock.lock();while(!waxed)condition.await();TimeUnit.MILLISECONDS.sleep(200);System.out.println("WaxOff");waxed=false;condition.signalAll();} finally{lock.unlock();}}}class WaxOn implements Runnable{private Car car;public WaxOn(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted())car.waxOn();}catch(InterruptedException e){System.out.println("Wax on Interrupt");}System.out.println("Exit Wax on");}}class WaxOff implements Runnable{private Car car;public WaxOff(Car car){this.car=car;}@Overridepublic void run() {// TODO Auto-generated method stubtry{while(!Thread.interrupted())car.waxOff();}catch(InterruptedException e){System.out.println("Wax off interrupt");}System.out.println("Exit Wax Off");}}public class WaxOMatic {public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stub ExecutorService exec = Executors.newCachedThreadPool(); Car car = new Car(); WaxOn waxOn = new WaxOn(car); WaxOff waxOff = new WaxOff(car); exec.execute(waxOn); exec.execute(waxOff); TimeUnit.SECONDS.sleep(5); exec.shutdownNow();}}
0 0
- 同步示例
- Java线程同步示例
- Java 线程同步示例
- java线程同步示例
- C# 线程同步示例
- Java线程同步示例
- 进程同步-死锁示例
- 线程同步示例
- 同步客户端套接字示例
- 同步两个数据库的示例
- 同步客户端套接字示例
- 同步服务器套接字示例
- TCP同步传送数据示例
- 线程同步的简单示例
- 【zookeeper】 同步API示例入门
- Elasticsearch+MySQL数据同步示例
- MMO位置同步及示例
- 进程同步示例代码(依据信号量达到同步)
- 27. Remove Element(Java)
- PropertyPlaceholderConfigurer类使用
- 表白专用
- Android使用SurfaceView代替AnimationDrawable播放多图帧动画,避免OOM和卡顿
- getchar()的灵活使用
- 同步示例
- 上课编程练习六
- NOR与NAND
- Android多线程ListView加载并缓冲图片
- 51nod 1072 威佐夫游戏
- 基于huffman的文件压缩
- 一步搞定无法审查元素
- centos 7 解决 systemctl dbus bug
- PostgreSQL学习第七篇--psql常用命令