Are you king of threads?
We were implementing a producer consumer thing and my partner dropped out leaving me with a huge load of problems.
Threads are not exactly my cup of tea. All I want to do is to make the application pause as long as the time printed from the keyboard.
The time pausing should be specified for consuming the products and producing them. Also how long the buffer should hold after an item is inserted there. I don't think I'm that far from the solution, but nothing happens when I try to run it.
I apologise for the amount of code, but maybe it was nessecary to get the whole picture?
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
publicclass Producerextends Location
implements Runnable
{
public Thread aktivitet=new Thread(this);
private ProductTypeList canProducera;
private ProductList harProducerat;
private Color color;
private String producerStatus="";
private String producerName;
private JPanel prodPanel;
private JPanel prodLetterHasPanel;
private Label prodSifTimerLabel =new Label ("0");
private Timer t;
private Label prodStatusLabel;
private Buffer buffer;
private SpelFrame frame;
public Producer(Color arg_color, String name, ProductTypeList canProduc,Buffer buffer1)
{
this.producerName=name;
this.color = arg_color;
this.canProducera= canProduc;
this.harProducerat=new ProductList();
this.producerStatus="WAITING_ON_BUFFER_NOT_FULL";
System.out.println(this.producerName +" ");
this.buffer= buffer1;
this.t =new Timer(100,new TickLyssnare(prodSifTimerLabel));
t.start();
aktivitet.start();
}
publicvoid addFrame(SpelFrame fr)
{
this.frame=fr;
}
publicvoid run()
{
while(!Thread.interrupted())
{
try
{
Thread.sleep(2000);
Product produkt= produce();
this.buffer.put(produkt);
}
catch(InterruptedException avbruten)
{
break;
}
}
}
public Color getProducerColor()
{
return this.color;
}
public ProductList getHasProducet()
{
return this.harProducerat;
}
private ProductType getRandomProductType()
{
int rand_tal= (int) (Math.random()*this.canProducera.getListLength());
return this.canProducera.getObjectByIndex(rand_tal);
}
public Product produce()
{
Product newProduct =new Product(this, this.getRandomProductType());
this.harProducerat.setProduct(newProduct);
this.producerStatus="PRODUCING";
this.prodStatusLabel.setText(this.producerStatus);
this.addProductTillProducerPanel(newProduct);
//this.buffer_Manipulation(newProduct);
return newProduct;
}
publicvoid buffer_Manipulation(Product product)
{
System.out.println("Remove fr錸 "+this.producerName +" "+product.getType().present_me()+
" till buffer");
if(buffer.is_Buffer_Full())
{
this.producerStatus="WAITING_ON_BUFFER_NOT_FULL";
this.prodStatusLabel.setText(this.producerStatus);
}
else
{
System.out.println("Remove fr錸 "+this.producerName +" "+product.getType().present_me()+
" till buffer");
this.producerStatus="MANIPULATING_BUFFER";
this.prodStatusLabel.setText(this.producerStatus);
product.moveToBuffer(this.buffer);
buffer.uppdateBufferInformationPanel();
}
}
public JPanel producerPanel()
{
this.prodPanel=new JPanel();
prodPanel.setLayout(new GridLayout(8,1));
Label prodLabel =new Label(this.producerName);
prodLabel.setForeground(Color.WHITE);
prodLabel.setBackground(this.color);
prodPanel.add(prodLabel);
this.prodStatusLabel =new Label(this.producerStatus);
prodPanel.add(prodStatusLabel);
Label prodCanLabel =new Label("Can produce: ");
prodPanel.add(prodCanLabel);
Label prodLetterCanLabel =new Label(canProducera.presenteraMe());
prodLetterCanLabel.setForeground(this.color);
prodPanel.add(prodLetterCanLabel);
Label prodTimerLabel =new Label ("Timer: ");
prodPanel.add(prodTimerLabel);
this.prodSifTimerLabel.setForeground(this.color);
prodPanel.add(this.prodSifTimerLabel);
Label prodHasLabel =new Label("Has produced:");
prodPanel.add(prodHasLabel);
this.prodLetterHasPanel =this.harProducerat.presenteraMeJPanel();
prodPanel.add(this.prodLetterHasPanel);
prodPanel.setBorder(BorderFactory.createLineBorder(this.color));
return this.prodPanel;
}
publicvoid addProductTillProducerPanel(Product product)
{
Label l =new Label(product.getType().present_me());
l.setForeground(this.color);
this.prodLetterHasPanel.add(l);
this.frame.setVisible(true);
}
class TickLyssnareimplements ActionListener
{
privateint tick;
privateint sec;
private Label lab;
public TickLyssnare(Label tidLabel)
{
this.lab= tidLabel;
this.tick= 0;
this.sec= 0;
}
publicvoid actionPerformed(ActionEvent e)
{
tick= tick + 1;
if (tick== 10)
{
tick= 0;
sec++;
System.out.println(" Tick Lyssnare: "+this.sec);
this.lab.setText(""+sec);
}
}
}
}
--
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
publicclass Consumerextends Location
implements Runnable
{
public Thread aktivitet =new Thread(this);
private ProductTypeList canConsum;
private ProductList hasConsumt;
private String consumerStatus ="";
private String consumerName;
private JPanel consPanel;
private Label consStatusLabel;
private Label consSifTimerLabel=new Label ("0");
private JPanel consLetterHasPanel;
private Timer t;
private SpelFrame frame;
private Buffer buffer;
privatelong tid;
public Consumer(String name,ProductTypeList argCanConsum, Buffer buffer1)
{
this.consumerName= name;
this.canConsum = argCanConsum;
this.hasConsumt =new ProductList();
this.consumerStatus ="WAITING_ON_BUFFER_ACCESS";
this.buffer= buffer1;
this.t =new Timer(100,new TickLyssnare(consSifTimerLabel));
t.start();
aktivitet.start();
}
publicvoid run()
{
while (!Thread.interrupted())
{
try{
Thread.sleep(2000);
buffer.take(this);
}
catch (InterruptedException avbruten)
{
break;
}
}
}
publicvoid addFrame(SpelFrame fr)
{
this.frame=fr;
}
publicvoid consumera(Buffer buffer)
{
if(!buffer.is_Buffer_Empty())
{
this.consumerStatus="MANIPULATING_BUFFER";
this.consStatusLabel.setText(this.consumerStatus);
ProductList bufferList= buffer.getBufferList();
for(int i=0; i< bufferList.getListLength();i++)
{
Product product= bufferList.getObjectByIndex(i);
if(this.isInCanComsum(product.getType()))
{
product.moveToConsumer(this);
this.addProductTillConsumerPanel(product);
System.out.println(product.getType().present_me()+" har consumerat");
buffer.uppdateBufferInformationPanel();
this.consumerStatus="CONSUMING";
this.consStatusLabel.setText(this.consumerStatus);
}
}
}
else
{
this.consumerStatus="WAITING_ON_BUFFER_NOT_EMPTY";
this.consStatusLabel.setText(this.consumerStatus);
}
}
publicvoid addProductTillConsumerPanel(Product product)
{
Label l =new Label(product.getType().present_me());
l.setForeground(product.getProducer().getProducerColor());
this.consLetterHasPanel.add(l);
this.frame.setVisible(true);
}
publicboolean isInCanComsum(ProductType prType)
{
return this.canConsum.isInTypeList(prType);
}
publicvoid addHasConsumt(Product product)
{
this.hasConsumt.setProduct(product);
}
publicvoid setConsumerStatus(String status)
{
this.consumerStatus=status;
}
public String getConsumerStatus()
{
return this.consumerStatus;
}
publicvoid consume(Product new_product)
{
this.hasConsumt.setProduct(new_product);
}
publicvoid buffer_Manipulation()
{
}
public JPanel consumerPanel()
{
this.consPanel=new JPanel();
consPanel.setLayout(new GridLayout(8,1));
Label consLabel =new Label(this.consumerName);
consLabel.setForeground(Color.WHITE);
consLabel.setBackground(Color.MAGENTA);
consPanel.add(consLabel);
this.consStatusLabel =new Label (this.consumerStatus);
consPanel.add(consStatusLabel);
Label consCanLabel =new Label("Can consume: ");
consPanel.add(consCanLabel);
Label consLetterCanLabel =new Label(canConsum.presenteraMe());
consPanel.add(consLetterCanLabel);
Label consHasLabel =new Label("Has consumed:");
consPanel.add(consHasLabel);
this.consLetterHasPanel =this.hasConsumt.presenteraMeJPanel();
consPanel.add(consLetterHasPanel);
Label consTimerLabel =new Label ("Timer: ");
consPanel.add(consTimerLabel);
consPanel.add(this.consSifTimerLabel);
consPanel.setBorder(BorderFactory.createLineBorder(Color.MAGENTA));
return this.consPanel;
}
class TickLyssnareimplements ActionListener
{
privateint tick;
privateint sec;
private Label lab;
public TickLyssnare(Label tidLabel)
{
this.lab= tidLabel;
this.tick= 0;
this.sec= 0;
}
publicvoid actionPerformed(ActionEvent e)
{
tick= tick + 1;
if (tick== 10)
{
tick= 0;
sec++;
System.out.println(" Tick Lyssnare: "+this.sec);
this.lab.setText(""+sec);
}
}
}
}
--
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
publicclass Bufferextends Location
{
private ProductList bufferList;
private String ss1;
private String ss2;
private Label l1,l2,l3, l4, lb1,lb2, lb3, lb4;
private DrawingArea dArea;
private SpelFrame frame;
publiclong tid;
public Buffer()
{
this.bufferList =new ProductList();
}
publicsynchronizedvoid put(Product product)
{
while (this.bufferList.getListLength()>=4)
while (bufferList.isEmty())
try{Thread.sleep(SpelFrame.setTime(tid));}
catch (InterruptedException e)
{
return;
}
product.getProducer().buffer_Manipulation(product);
notify();
this.frame.setVisible(true);
}
publicsynchronizedvoid take(Consumer consumer)
{
while (this.bufferList.isEmty())
try{Thread.sleep(SpelFrame.setTime(tid));}
catch (InterruptedException e)
{
return;
}
consumer.consumera(this);
this.frame.setVisible(true);
}
publicvoid uppdateBufferInformationPanel()
{
if(this.is_Buffer_Empty())
{
ss1 ="* is empty";
}
else
{
ss1 ="* is not empty";
}
if(this.is_Buffer_Full())
{
ss2 ="* is full";
}
else
{
ss2 ="* is not full";
}
this.dArea.changeDrawingArea(ss1, ss2,"Buffer");
int buffLength= bufferList.getListLength();
if (buffLength>0)
{
lb1.setText(bufferList.getObjectByIndex(0).present_me());
lb1.setForeground(
bufferList.getObjectByIndex(0).getProducer().getProducerColor());
}
else
{
lb1.setText("");
}
if (buffLength>1)
{
lb2.setText(bufferList.getObjectByIndex(1).present_me());
lb2.setForeground(
bufferList.getObjectByIndex(1).getProducer().getProducerColor());
}
else
{
lb2.setText("");
}
if (buffLength>2)
{
lb3.setText(bufferList.getObjectByIndex(2).present_me());
lb3.setForeground(
bufferList.getObjectByIndex(2).getProducer().getProducerColor());
}
else
{
lb3.setText("");
}
if (buffLength>3)
{
lb4.setText(bufferList.getObjectByIndex(3).present_me());
lb4.setForeground(
bufferList.getObjectByIndex(3).getProducer().getProducerColor());
}
else
{
lb4.setText("");
}
}
public ProductList getBufferList()
{
return this.bufferList;
}
publicboolean is_Buffer_Empty()
{
return this.bufferList.isEmty();
}
publicboolean is_Buffer_Full()
{
if(this.bufferList.isEmty())
{
returnfalse;
}
if(this.bufferList.getListLength()==4)
{
returntrue;
}
elsereturnfalse;
}
publicvoid addToBuffer(Product product)
{
bufferList.setProduct(product);
System.out.println(product.getType().present_me()+" har addit till buffer ");
}
publicvoid deleteFromBuffer(Product product)
{
this.bufferList.remove_Product(product);
System.out.println(product.getType().present_me()+" has removed from the buffer");
}
public JPanel presentMePanel()
{
JPanel bufferPanel =new JPanel();
bufferPanel.setLayout(new GridLayout(2,1));
if(this.is_Buffer_Empty())
{
ss1 ="* is empty";
}
else
{
ss1 ="* is not empty";
}
if(this.is_Buffer_Full())
{
ss2 ="* is full";
}
else
{
ss2 ="* is not full";
}
this.dArea =new DrawingArea(ss1, ss2,"Buffer");
bufferPanel.add(dArea);
JPanel bufferInnePanel =new JPanel();
bufferInnePanel.setLayout(new GridLayout(8,1));
JPanel bufferStorInnePanel =new JPanel();
bufferStorInnePanel.setLayout(new GridLayout(1,3));
int buffLength= bufferList.getListLength();
l1 =new Label("Product 1");
bufferInnePanel.add(l1);
if (buffLength>0)
{
lb1 =new Label(bufferList.getObjectByIndex(0).present_me());
lb1.setForeground(
bufferList.getObjectByIndex(0).getProducer().getProducerColor());
}
else
{
lb1 =new Label("");
}
bufferInnePanel.add(lb1);
l2 =new Label("Product 2");
bufferInnePanel.add(l2);
if (buffLength>1)
{
lb2 =new Label(bufferList.getObjectByIndex(1).present_me());
lb2.setForeground(
bufferList.getObjectByIndex(1).getProducer().getProducerColor());
}
else
{
lb2 =new Label("");
}
bufferInnePanel.add(lb2);
l3 =new Label("Product 3");
bufferInnePanel.add(l3);
if (buffLength>2)
{
lb3 =new Label(bufferList.getObjectByIndex(2).present_me());
lb3.setForeground(
bufferList.getObjectByIndex(2).getProducer().getProducerColor());
}
else
{
lb3 =new Label("");
}
bufferInnePanel.add(lb3);
l4 =new Label("Product 4");
bufferInnePanel.add(l4);
if (buffLength>3)
{
lb4 =new Label(bufferList.getObjectByIndex(3).present_me());
lb4.setForeground(
bufferList.getObjectByIndex(3).getProducer().getProducerColor());
}
else
{
lb4 =new Label("");
}
bufferInnePanel.add(lb4);
bufferInnePanel.setBorder(BorderFactory.createLineBorder(Color.MAGENTA));
WhiteRectangel rect1=new WhiteRectangel();
WhiteRectangel rect2=new WhiteRectangel();
bufferStorInnePanel.add(rect1);
bufferStorInnePanel.add(bufferInnePanel);
bufferStorInnePanel.add(rect2);
bufferPanel.add(bufferStorInnePanel);
return bufferPanel;
}
publicvoid addFrame(SpelFrame fr)
{
this.frame=fr;
}
}
Message was edited by:
_Jasper_

