新线程库也有跟老线程库对应的方法,新线程库有线程安全的高效队列。没有上面麻烦,但上面写的是理解新线程数据结构与实现的基础。package com.zhidao;import java.util.LinkedList;import java.util.List;import java.util.Random;public class TestThread2 {//缓冲上限private long bufsize;//缓冲private...
课程设计题目,多线程编程:医院门诊模拟,想用java实现,求大神指点
典型的生产者消费者模型。
了解j5的并发库,那个并发库中有适合组件实现。
如果不了解,这么来:
创建一个队列,此队列要求线程安全,如果队列为空则消费者阻塞。如果队列达到某个最大值,则阻塞生产者。
队列用,普通的list或实现好的队列包装成线程安全的。
用synchronized同步原方法或代码块。
写一个或n个线程,模拟病人,排队办理业务,往上面的队列中添加数据。
当达到队列的最大容积,阻塞,等待生产者线程取数据。
阻塞:makerLock.wait();//虚拟机会出让线程挂起,其实就是操作系统,保存当前线程在cpu上的运行状态。再出让线程正在使用的cpu资源,占用的内存不会释放。
往队列插入数据的时候,因为不知道是否有消费者处于等待状态,通知消费者:
customerLock.notifyAll();//虚拟机调度消费者线程运行,实际上是操作系统,把保存的消费者线程状态,从新加载到cpu中接着运行。接着运行线程是任意的,取决于不同操作系统的线程调度算法。
消费者线程读取一个数据后,要通知生产者,可以继续,道理同上:
makerLock.notifyAll();
队列中,无数据可读的时候:
customerLock.wait();//原理同上,
最后注意,生产者跟消费者使用了两个不同的对象锁。lock.wait()的使用方法是这样的:
synchronized(lock){
... ...
while(condition == true) {
lock.wait();
}
... ...
Object o = queen.pop();
lock.notifyAll();
}
最后启动n个线程读队列,模拟办理业务的窗口;n个线程写队列,模拟病人排队。
新线程库也有跟老线程库对应的方法,新线程库有线程安全的高效队列。没有上面麻烦,但上面写的是理解新线程数据结构与实现的基础。
package com.zhidao;import java.util.LinkedList;import java.util.List;import java.util.Random;public class TestThread2 {//缓冲上限private long bufsize;//缓冲private List<String> buf;public TestThread2() {bufsize = 5;buf = new LinkedList<String>();}//生产者调用public void put(String s) {//模拟生产者跟不上消费者/*try {Thread.sleep(100);} catch (InterruptedException e) {}*/synchronized(this) {//超过队列限制就等待while(buf.size() == bufsize) {System.out.println("队列已满,生产者:"+Thread.currentThread().getId()+"开始等待。");try {this.wait();} catch (InterruptedException e) {}}buf.add(s);//通知消费者this.notifyAll();}}//消费者调用synchronized public String take() {//模拟消费者跟不上生产者try {Thread.sleep(100);} catch (InterruptedException e) {}String s = null;synchronized(this) {while(buf.size() == 0) {System.out.println("队列为空,消费者:"+Thread.currentThread().getId()+"开始等待。");try {this.wait();} catch (InterruptedException e) {}}//取先放入的元素,并移除s= buf.get(0);buf.remove(0);//通知生产者this.notifyAll();}return s;}public static void main(String[] args) {//自己实现的,安全队列final TestThread2 tt = new TestThread2();//生产者Thread p = new Thread(new Runnable(){@Overridepublic void run() {while(!Thread.currentThread().isInterrupted()) {Random r = new Random();tt.put(String.valueOf(r.nextInt(10)));}}});//消费者Thread c1 = new Thread(new Runnable(){@Overridepublic void run() {while(!Thread.currentThread().isInterrupted()) {System.out.println("线程:"+Thread.currentThread().getId()+"获取到数据"+tt.take());}}});Thread c2 = new Thread(new Runnable(){@Overridepublic void run() {while(!Thread.currentThread().isInterrupted()) {System.out.println("线程:"+Thread.currentThread().getId()+"获取到数据"+tt.take());}}});p.start();c1.start();c2.start();try {p.join();c1.join();c2.join();} catch (InterruptedException e) {}}}2017-11-19
驳回原因都是什么2017-11-19