求各位JAVA大神,帮做下这道题吧,下周要交的。。。
多线程编程编写生产者/消费者模式程序:生产者向堆栈中压数据,消费者从堆栈中取数据。生产者和消费者共同使用同一个堆栈。要求:生产者每隔600ms产生一个0~999之间的一个...
多线程编程
编写生产者/消费者模式程序:生产者向堆栈中压数据,消费者从堆栈中取数据。生产者和消费者共同使用同一个堆栈。
要求:生产者每隔600ms产生一个0~999之间的一个随机数,保存在堆栈中,一共200个。消费者从堆栈中取出200个数据,间隔500ms。试编写堆栈类,编写创建消费者、生产者的线程,并演示两个生产者线程,三个消费者线程运行过程。 展开
编写生产者/消费者模式程序:生产者向堆栈中压数据,消费者从堆栈中取数据。生产者和消费者共同使用同一个堆栈。
要求:生产者每隔600ms产生一个0~999之间的一个随机数,保存在堆栈中,一共200个。消费者从堆栈中取出200个数据,间隔500ms。试编写堆栈类,编写创建消费者、生产者的线程,并演示两个生产者线程,三个消费者线程运行过程。 展开
1个回答
展开全部
package StackDemo;
import java.util.Random;
import java.util.Stack;
public class StackDemo {
private static Stack<Integer> stack = new Stack<Integer>();
class Producer implements Runnable {
private String name;
private Random random = new Random();
private int data;
public Producer(String name) {
this.name = name;
}
public synchronized void run() {
while (true) {
if (StackDemo.stack.size() < 200) {
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
data = random.nextInt(1000);
StackDemo.stack.push(data);
System.out.println(this.name + "生产了" + data + " 堆栈里还剩下"
+ StackDemo.stack.size() + "个数据");
}
}
}
}
class Customer implements Runnable {
private String name;
public Customer(String name) {
this.name = name;
}
public synchronized void run() {
while (true) {
if (!StackDemo.stack.isEmpty()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
int data = StackDemo.stack.pop();
System.out.println(this.name + "消耗了" + data + " 堆栈里还剩下"
+ StackDemo.stack.size() + "个数据");
}
}
}
}
public static void main(String[] args) {
StackDemo stackDemo = new StackDemo();
Producer p1 = stackDemo.new Producer("生产者1");
Producer p2 = stackDemo.new Producer("生产者2");
Customer t1 = stackDemo.new Customer("消费者1");
Customer t2 = stackDemo.new Customer("消费者2");
Customer t3 = stackDemo.new Customer("消费者3");
new Thread(p1).start();
new Thread(p2).start();
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
import java.util.Random;
import java.util.Stack;
public class StackDemo {
private static Stack<Integer> stack = new Stack<Integer>();
class Producer implements Runnable {
private String name;
private Random random = new Random();
private int data;
public Producer(String name) {
this.name = name;
}
public synchronized void run() {
while (true) {
if (StackDemo.stack.size() < 200) {
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
data = random.nextInt(1000);
StackDemo.stack.push(data);
System.out.println(this.name + "生产了" + data + " 堆栈里还剩下"
+ StackDemo.stack.size() + "个数据");
}
}
}
}
class Customer implements Runnable {
private String name;
public Customer(String name) {
this.name = name;
}
public synchronized void run() {
while (true) {
if (!StackDemo.stack.isEmpty()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
int data = StackDemo.stack.pop();
System.out.println(this.name + "消耗了" + data + " 堆栈里还剩下"
+ StackDemo.stack.size() + "个数据");
}
}
}
}
public static void main(String[] args) {
StackDemo stackDemo = new StackDemo();
Producer p1 = stackDemo.new Producer("生产者1");
Producer p2 = stackDemo.new Producer("生产者2");
Customer t1 = stackDemo.new Customer("消费者1");
Customer t2 = stackDemo.new Customer("消费者2");
Customer t3 = stackDemo.new Customer("消费者3");
new Thread(p1).start();
new Thread(p2).start();
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
更多追问追答
追问
好像有个地方出错了,怎么改呀???
追答
线程上有点冲突……等我回头改改
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |