Java上路10-线程通信
来源:互联网 发布:疯狂java讲义电子版 编辑:程序博客网 时间:2024/06/06 18:27
需求:超市的销售部和采购部同时对仓库进行使用,库存达到100,销售部就卖货,库存小于1000就必须进货。开始运营超市。
代码:
class Stock //库存{ public int count=0; //库存数量} class Procure implements Runnable //采购{ private Stock sk; Procure(Stock sk) { this.sk=sk; } public void run() { while (true) { synchronized (sk) { if(sk.count<1000) { sk.count +=100; System.out.println("存货100,余量 "+sk.count); } } } }} class Sales implements Runnable //销售{ private Stock sk; Sales (Stock sk) { this.sk=sk; } public void run() { while (true) { synchronized (sk) { if(sk.count>=100) { sk.count-= 100; System.out.println("出货100,余 "+sk.count); } } } }} class ThreadCommun{ public static void main(String[] args) { Stock sk=new Stock(); Procure pr=new Procure(sk); Sales sa=new Sales(sk); new Thread(pr).start(); new Thread(sa).start(); }}
现在要求,销售部卖出100,采购部就得进货100;采购部一进货,销售部就得卖;交替执行。我们设置一个销售开关,如果开,则采购等待,销售卖货,卖出后关闭开关;如果关,则销售等待,采购进货,进货后打开开关。
如果采购部和销售部都有多个员工同步进行工作。如何做到采购100,销售100?
class Stock //库存{ private int count=100; //库存数量 public boolean sale=true; //销售 public synchronized void set(int count) { while (sale) try{this.wait();}catch(Exceptione){} this.count += count; System.out.println("加入了 "+count+",余"+this.count); sale=true; this.notifyAll(); } public synchronized void show() { while (!sale) try{this.wait();}catch(Exceptione){} count -= 100; System.out.println("销售 100,余"+count); sale=false; this.notifyAll(); }} class Procure implements Runnable //采购{ private Stock sk; Procure(Stock sk) { this.sk=sk; } public void run() { while (true) { sk.set(100); } }} class Sales implements Runnable //销售{ private Stock sk; Sales (Stock sk) { this.sk=sk; } public void run() { while (true) { sk.show(); } }} class ThreadCommun{ public static void main(String[] args) { Stock sk=new Stock(); //仓库 new Thread(new Procure(sk)).start(); new Thread(new Procure(sk)).start(); //new Thread(new Procure(sk)).start(); new Thread(new Sales(sk)).start(); new Thread(new Sales(sk)).start(); new Thread(new Sales(sk)).start(); }}
JDK1.5中提供了另一套类实现同步。java.util.concurrent.locks,其中有个接口Lock。他可以为不同的线程指定不同的通道,并且所有线程是同步的。
import java.util.concurrent.locks.*;//导入这个类包class LockDemo {public static void main(String[] args) {Stock sk=new Stock();new Thread(new Procure(sk)).start();new Thread(new Sales(sk)).start();new Thread(new Procure(sk)).start();new Thread(new Sales(sk)).start();}}class Procure implements Runnable//采购{private Stock sk;Procure(Stock sk){this.sk=sk;}public void run(){while (true){try{sk.inner(100);}catch(InterruptedException e){}}}}class Sales implements Runnable//销售{private Stock sk;Sales (Stock sk){this.sk=sk;}public void run(){while (true){try{sk.outer();}catch(InterruptedException e){}}}}class Stock//操作库存{private int count=100;//余量private boolean flag=true;//销售private Lock lock=new ReentrantLock();private Condition condition_in =lock.newCondition();private Condition condition_out =lock.newCondition();void inner (int count) throws InterruptedException{lock.lock();try{while (flag)condition_in.await();this.count+=count;System.out.println(Thread.currentThread().getName()+"加入了 "+count+",余 "+this.count);flag=true;condition_out.signal();}finally{lock.unlock();}}void outer () throws InterruptedException{lock.lock();try{while (!flag)condition_out.await();count -= 100;System.out.println(Thread.currentThread().getName()+"销售 100,余"+count);flag=false;condition_in.signal();}finally{lock.unlock();}}}
针对线程同时处于冻结状态,Java提供interrupt方法强制线程进入运行状态。
class StopThreadDemo {public static void main(String[] args) {StopThread st=new StopThread();Thread t1=new Thread(st);Thread t2=new Thread(st);t1.start();t2.start();int num=0;while (true){if (num++==60){t1.interrupt();//强制唤醒线程t2.interrupt();break;}System.out.println("main..."+num);}}}class StopThread implements Runnable{private boolean flag=true;public synchronized void run(){while (flag){try{wait();}catch (InterruptedException e){System.out.println(Thread.currentThread().getName()+"...Exception");flag=false;//结束循环,完成run,线程终止}System.out.println(Thread.currentThread().getName()+"...run");}}}
- Java上路10-线程通信
- java线程上路
- java新手上路(五):线程,多线程
- JAVA新手上路之遇见线程
- JAVA线程-线程间通信
- java线程通信
- java 线程间通信
- Java.线程通信
- java线程通信
- java线程通信复习
- Java 线程通信
- Java线程通信机制
- java线程间通信
- Java线程通信 (一)
- java 线程通信
- Java线程通信
- Java线程间通信
- java 线程通信
- Python 数据挖掘推荐模块
- 【魔豆开发者活动】黑客马拉松.创业者活动[杭州.福云咖啡]12月15-16
- 创建线程的两种方式
- java 线程池
- If you want do something done,do it yourself。
- Java上路10-线程通信
- Web工程师必备的18款工具
- 找出10~40之间不能被2和3整除的整数。
- How to install SmartSVN on Ubuntu 10.10
- 狗日的开源软件许可证
- iPhone实战:定位与地图
- Android应用开发提高了电子产品的全新形象
- 极光推送技术原理:移动无线网络长连接
- Activity和Intent机制学习笔记