0
所以這段代碼在main中給出了多個錯誤。有人能告訴我這有什麼問題嗎?第一次海報所以請溫柔卡住多線程java
問題是要創建消費者和生產者線程做燈。只有兩個消費者是允許的,那裏有一個生產者線程的燈的每一個組件
public class ProducerConsumerTest{
public static void main(String[] args){
StartProducer startproducer = new StartProducer();
System.out.println("Start producer calling");
startproducer.start();
}
}
class StartProducer extends Thread{
Screw screw = new Screw();
Base base = new Base();
Stand stand = new Stand();
Socket socket = new Socket();
LightBulb lightbulb = new LightBulb();
int screws = screw.screwcount;
int bases = base.basecount;
int stands = stand.standcount;
int sockets = socket.socketcount;
int lightbulbs = lightbulb.bulbcount;
Consumer1 consumer1 = new Consumer1();
Consumer2 consumer2 = new Consumer2();
public synchronized void run(){
System.out.println("producer test");
screw.start();
base.start();
stand.start();
socket.start();
lightbulb.start();
boolean possible = possibleToBuildLamp(screws,bases,stands,sockets,lightbulbs);
if(possible == true){
consumer1.start();
consumer2.start();
}
}
private boolean possibleToBuildLamp(int screws, int bases, int stands,
int sockets, int lightbulbs) {
if(screws>=4 && bases>=1 && stands>=1 && sockets>=3 && lightbulbs>=3){
return true;
}
else return false;
}
}
class Screw extends Thread{
public int screwcount;
public synchronized void run(){
while(true){
if(screwcount>=0 && screwcount<7){
screwcount++;
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Base extends Thread{
public int basecount;
public synchronized void run(){
while(true){
if(basecount>=0 && basecount <2){
basecount++;
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Stand extends Thread {
public int standcount;
public synchronized void run(){
while(true){
if(standcount>=0 && standcount<2){
standcount++;
}
}
}
}
class Socket extends Thread {
public int socketcount;
public synchronized void run(){
while(true){
if(socketcount>=0 && socketcount<7){
socketcount++;
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class LightBulb extends Thread {
public int bulbcount;
public synchronized void run(){
while(true){
if(bulbcount>=0 && bulbcount<4){
bulbcount++;
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer1 extends Thread {
ProducerConsumerTest test = new ProducerConsumerTest();
StartProducer start = new StartProducer();
public synchronized void run(){
while(true){
Screw screw = new Screw();
Base base = new Base();
Stand stand = new Stand();
Socket socket = new Socket();
LightBulb lightbulb = new LightBulb();
screw.screwcount-=4;
base.basecount-=1;
stand.standcount-=1;
socket.socketcount-=3;
lightbulb.bulbcount-=3;
System.out.println("one lamp made");
start.notify();
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer2 extends Thread {
ProducerConsumerTest test = new ProducerConsumerTest();
StartProducer start = new StartProducer();
public synchronized void run(){
while(true){
Screw screw = new Screw();
Base base = new Base();
Stand stand = new Stand();
Socket socket = new Socket();
LightBulb lightbulb = new LightBulb();
screw.screwcount-=4;
base.basecount-=1;
stand.standcount-=1;
socket.socketcount-=3;
lightbulb.bulbcount-=3;
System.out.println("One lamp made");
start.notify();
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
*「第一次張貼海報,所以請溫柔」* - 輕輕地:不要只是說「這段代碼給主要多個錯誤」,你應該說錯誤是什麼,或者至少是什麼樣的錯誤是。 – 2014-11-02 01:55:14