多线程 同步问题
2015-06-24 23:06
120 查看
需求一
模拟四个窗口同时卖100张火车票的问题
对于卖票的过程,必须使用同步代码段,否则就会出现同一张票被多次售卖的问题
需求二
生产者消费者问题
需求三
使用监视器实现生产者消费者问题
模拟四个窗口同时卖100张火车票的问题
package work.thread; import java.util.Objects; /** * Created by coolkid on 2014/12/17 0017. */ class Ticket implements Runnable{ //票数 private int count = 100; private Object object = new Object(); private void saleTicket(){ while (true){ synchronized (object){ if (count > 0){ count--; System.out.println(Thread.currentThread().getName()+" 卖出第 "+(100-count)+" 张票"); }else { break; } } } } @Override public void run() { saleTicket(); } } public class Thread01 { public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); Thread t3 = new Thread(t); Thread t4 = new Thread(t); t1.start(); t2.start(); t3.start(); t4.start(); } }
对于卖票的过程,必须使用同步代码段,否则就会出现同一张票被多次售卖的问题
需求二
生产者消费者问题
package work.thread; /** * Created by coolkid on 2014/12/17 0017. */ class Resource02{ private String name; private String sex; private int count = 0; public synchronized void set(String name,String sex){ while (count>10){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name; this.sex = sex; count++; this.notifyAll(); } public synchronized void out() { while (count<1){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+"\tname = " + name+"\nsex = " + sex); count--; this.notifyAll(); } } class Input02 implements Runnable{ private Resource02 r; public Input02(Resource02 r) { this.r = r; } @Override public void run() { int x = 0; while (true){ if (x == 0){ r.set("mike","nan"); }else { r.set("莉莉","女.........."); } x = (x+1)%2; } } } class Output02 implements Runnable{ private Resource02 r; public Output02(Resource02 r) { this.r = r; } @Override public void run() { while (true){ r.out(); } } } public class Thread02 { public static void main(String[] args) { //资源 Resource02 r = new Resource02(); //生产者 Input02 i = new Input02(r); //消费者 Output02 o = new Output02(r); //执行路径 Thread t1 = new Thread(i); Thread t2 = new Thread(i); Thread t3 = new Thread(o); t1.start(); t2.start(); t3.start(); } }
需求三
使用监视器实现生产者消费者问题
package work.thread; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Created by coolkid on 2014/12/17 0017. */ class Resource02{ private String name; private String sex; private int count = 0; Lock lock = new ReentrantLock(); Condition pro_con = lock.newCondition(); Condition con_con = lock.newCondition(); public void set(String name,String sex){ lock.lock(); try { while (count>10){ pro_con.await(); } this.name = name; this.sex = sex; count++; con_con.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void out() { lock.lock(); try { while (count<1){ con_con.await(); } System.out.println(Thread.currentThread().getName()+"\tname = " + name+"\tsex = " + sex); count--; pro_con.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } class Input02 implements Runnable{ private Resource02 r; public Input02(Resource02 r) { this.r = r; } @Override public void run() { int x = 0; while (true){ if (x == 0){ r.set("mike","nan"); }else { r.set("莉莉","女.........."); } x = (x+1)%2; } } } class Output02 implements Runnable{ private Resource02 r; public Output02(Resource02 r) { this.r = r; } @Override public void run() { while (true){ r.out(); } } } public class Thread02 { public static void main(String[] args) { //资源 Resource02 r = new Resource02(); //生产者 Input02 i = new Input02(r); //消费者 Output02 o = new Output02(r); //执行路径 Thread t1 = new Thread(i); Thread t2 = new Thread(i); Thread t3 = new Thread(o); t1.start(); t2.start(); t3.start(); } }
相关文章推荐
- 谈谈Ext JS的组件――布局的使用方法
- Android SDK Manager - SDK下载说明
- iOS-KVO究竟怎么用
- 自动开发环境
- 实验四 用窗函数法设计FIR滤波器
- JAVA中list,set,数组之间的转换详解
- 基本的Sql编写注意事项
- 如何弹出QQ临时对话框实现不添加好友在线交谈效果
- 背包问题
- TCP/IP简介
- 第1讲 zend1.11介绍
- u-boot2010.03 移植篇(四)-----支持DM9000,实现tftp下载
- RecyclerView全解(一)
- ImageView的selector效果
- Python输出字符串或文件颜色显示
- 红黑树学习
- POJ 2376 Cleaning shifts 贪心 基础题
- 第三题
- C++——STL中三种顺序容器的简要区别
- using的几种用法