java生产者消费者问题使线程等待其他线程完成后再完成
我目前正在做一个多线程的生产者-消费者问题。一开始有1000个字节可用,使用RAM-nad驱动程序占用了500个字节,让我处理500个线程。共有4家生产商,如下所示:java生产者消费者问题使线程等待其他线程完成后再完成,java,multithreading,time,synchronization,producer-consumer,Java,Multithreading,Time,Synchronization,Producer Consumer,我目前正在做一个多线程的生产者-消费者问题。一开始有1000个字节可用,使用RAM-nad驱动程序占用了500个字节,让我处理500个线程。共有4家生产商,如下所示: 一个线程,用于启动10秒的BubbleWitch2会话,每个会话需要100字节的RAM 第二 启动Spotify流20秒的线程,每秒需要250字节的RAM 系统线程和管理线程,它们一起需要每秒50字节的RAM,以及 一旦调用,执行一段随机长度的时间 安装2 KB新安全更新的线程,该更新将存储到磁盘,安装时每秒需要150字节的RAM
/**
* Created by User on 10/08/2014.
*/
public class ProducerConsumerTest {
public static void main(String[] args) throws InterruptedException {
Buffer c = new Buffer();
BubbleWitch2 p1 = new BubbleWitch2(c, 1);
Processor c1 = new Processor(c, 2);
Spotify p2 = new Spotify(c, 3);
SystemManagement p3 = new SystemManagement(c,4);
securityUpdate p4 = new securityUpdate(c,5, p1,p2,p3);
p1.setName("BubbleWitch2 ");
p2.setName("Spotify ");
p3.setName("System Management ");
p4.setName("Security Update ");
c1.start();
p1.start();
p2.start();
p3.start();
p4.start();
p2.join();
p3.join();
p4.join();
System.exit(0);
}
}
我的缓冲区/小隔间类
/**
* Created by User on 10/08/2014.
*/
class Buffer {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
}
catch (InterruptedException e) {
}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
}
catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}
我的消费者阶层
class Processor extends Thread {
private Buffer cubbyhole;
private int number;
public Processor(Buffer c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 60; i++) {
value = cubbyhole.get();
System.out.println("Processor #"
+ this.number
+ " got: " + value);
}
}
}
类处理器扩展线程{
私人缓冲隔间;
私有整数;
公共处理器(缓冲区c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
int值=0;
对于(int i=0;i<60;i++){
value=cubbyhole.get();
System.out.println(“处理器#”
+这个号码
+“得到:”+价值);
}
}
}
我的spotify制作人课程
class Spotify extends Thread {
private Buffer buffer;
private int number;
private int bytes;
public Spotify(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 20; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("Spotify has finished executing.");
System.out.println("*****************************");
}
}
import java.lang.*;
import java.lang.System;
/**
* Created by User on 10/08/2014.
*/
class BubbleWitch2 extends Thread {
private Buffer buffer;
private int number;
private int bytes;
public BubbleWitch2(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("BubbleWitch2 has finished executing.");
System.out.println("*****************************");
}
}
class SystemManagement extends Thread {
private Buffer buffer;
private int number, min = 1, max = 15;
private int loopCount = (int) (Math.random() * ( max - min ));
public SystemManagement(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < loopCount; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("System Management has finished executing.");
System.out.println("*****************************");
}
}
类Spotify扩展线程{
专用缓冲区;
私有整数;
私有整数字节;
公共Spotify(缓冲区c,整数){
缓冲区=c;
这个数字=数字;
}
公开募捐{
对于(int i=0;i<20;i++){
缓冲区。放置(i);
System.out.println(getName()+this.number
+“put:”+i);
试一试{
睡眠(1000);
}捕获(中断异常e){}
}
System.out.println(“*************************************”);
System.out.println(“Spotify已完成执行”);
System.out.println(“*************************************”);
}
}
我的泡泡舞制作人班
class Spotify extends Thread {
private Buffer buffer;
private int number;
private int bytes;
public Spotify(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 20; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("Spotify has finished executing.");
System.out.println("*****************************");
}
}
import java.lang.*;
import java.lang.System;
/**
* Created by User on 10/08/2014.
*/
class BubbleWitch2 extends Thread {
private Buffer buffer;
private int number;
private int bytes;
public BubbleWitch2(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("BubbleWitch2 has finished executing.");
System.out.println("*****************************");
}
}
class SystemManagement extends Thread {
private Buffer buffer;
private int number, min = 1, max = 15;
private int loopCount = (int) (Math.random() * ( max - min ));
public SystemManagement(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < loopCount; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("System Management has finished executing.");
System.out.println("*****************************");
}
}
import java.lang.*;
导入java.lang.System;
/**
*用户于2014年8月10日创建。
*/
类BubbleWitch2扩展了线程{
专用缓冲区;
私有整数;
私有整数字节;
公共BubbleWitch2(缓冲区c,整数){
缓冲区=c;
这个数字=数字;
}
公开募捐{
对于(int i=0;i<10;i++){
缓冲区。放置(i);
System.out.println(getName()+this.number
+“put:”+i);
试一试{
睡眠(1000);
}捕获(中断异常e){}
}
System.out.println(“*************************************”);
System.out.println(“BubbleWitch2已完成执行”);
System.out.println(“*************************************”);
}
}
我的系统管理制作类
class Spotify extends Thread {
private Buffer buffer;
private int number;
private int bytes;
public Spotify(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 20; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("Spotify has finished executing.");
System.out.println("*****************************");
}
}
import java.lang.*;
import java.lang.System;
/**
* Created by User on 10/08/2014.
*/
class BubbleWitch2 extends Thread {
private Buffer buffer;
private int number;
private int bytes;
public BubbleWitch2(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("BubbleWitch2 has finished executing.");
System.out.println("*****************************");
}
}
class SystemManagement extends Thread {
private Buffer buffer;
private int number, min = 1, max = 15;
private int loopCount = (int) (Math.random() * ( max - min ));
public SystemManagement(Buffer c, int number) {
buffer = c;
this.number = number;
}
public void run() {
for (int i = 0; i < loopCount; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("System Management has finished executing.");
System.out.println("*****************************");
}
}
类系统管理扩展线程{
专用缓冲区;
私有整数,最小值=1,最大值=15;
私有int loopCount=(int)(Math.random()*(max-min));
公共系统管理(缓冲区c,整数){
缓冲区=c;
这个数字=数字;
}
公开募捐{
for(int i=0;i
我的安全更新类
/**
* Created by User on 14/08/2014.
*/
import java.lang.*;
import java.lang.System;
/**
* Created by User on 11/08/2014.
*/
class securityUpdate extends Thread {
private Buffer buffer;
private int number;
private int bytes = 150;
private int process = 0;
public securityUpdate (Buffer c, int number, BubbleWitch2 bubbleWitch2, Spotify spotify, SystemManagement systemManagement) throws InterruptedException {
buffer = c;
this.number = number;
bubbleWitch2.join();
spotify.join();
systemManagement.join();
}
public void run() {
for (int i = 0; i < 10; i++) {
buffer.put(i);
System.out.println(getName() + this.number
+ " put: " + i);
try {
sleep(1000);
} catch (InterruptedException e) { }
}
System.out.println("*****************************");
System.out.println("Security Update has finished executing.");
System.out.println("*****************************");
}
}
/**
*用户于2014年8月14日创建。
*/
导入java.lang.*;
导入java.lang.System;
/**
*用户于2014年8月11日创建。
*/
类securityUpdate扩展了线程{
专用缓冲区;
私有整数;
私有int字节=150;
私有int进程=0;
公共安全更新(缓冲区c、整数、BubbleWitch2、BubbleWitch2、Spotify Spotify、SystemManagement SystemManagement)引发中断异常{
缓冲区=c;
这个数字=数字;
bubbleWitch2.join();
spotify.join();
systemManagement.join();
}
公开募捐{
对于(int i=0;i<10;i++){
缓冲区。放置(i);
System.out.println(getName()+this.number
+“put:”+i);
试一试{
睡眠(1000);
}捕获(中断异常e){}
}
System.out.println(“*************************************”);
System.out.println(“安全更新已完成执行”);
System.out.println(“*************************************”);
}
}
我希望能够在不硬编码计数中的不同数字的情况下使其最后运行,因为我需要在代码中计算以每秒150字节的速度以2000字节的大小运行所需的时间,这将使硬编码变得无关紧要。有人有什么想法吗?我不确定我是否完全理解这个问题,但您正在加入
securityUpdate
的构造函数中的线程:
bubbleWitch2.join();
spotify.join();
systemManagement.join();
这些连接发生在线程启动之前,因此它们不是