Sunday, February 6, 2011

Threading Producer and Consumer Example

package prodcongui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener
{
  private JButton start;
  private JPanel button_panel, prod_panel, con_panel;
  private JLabel prod=new JLabel("Producer is not producing.");
  private JLabel con=new JLabel("Consumer is not consuming.");
  private Container cp;
  private Thread producer=new Thread(new Runner(),"Producer");
  private Thread consumer=new Thread(new Runner2(),"Consumer");
  private int buffer[]={-1,-1,-1,-1};
  private int buffer_count=0, sum=0, read=0, write=0;
  public GUI()
  {
    super("Producer Consumer");
    cp=getContentPane();
    start=new JButton("Start Threads");
    button_panel=new JPanel();
    prod_panel=new JPanel();
    con_panel=new JPanel();
    button_panel.add(start);
    cp.add(button_panel,BorderLayout.SOUTH);
    prod_panel.add(prod);
    con_panel.add(con);
    cp.add(prod_panel,BorderLayout.NORTH);
    cp.add(con_panel,FlowLayout.CENTER);
    prod_panel.setBackground(Color.RED);
    con_panel.setBackground(Color.RED);
    start.addActionListener(this);
    setSize(330,200);
    setLocation(330,330);
    setResizable(false);
    setVisible(true);
  }
  public static void main(String[] args)
  {
    GUI app=new GUI();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public synchronized void set(int value)
  {
    while(buffer_count==buffer.length)
    {
      try
      {
        SwingUtilities.invokeLater(new ChangeProdCon("Producer can't produce "+"\n"+
                                                  "Producer must wait.",prod,Color.ORANGE,prod_panel));
        wait();
      }
      catch(InterruptedException e){}
    }
    SwingUtilities.invokeLater(new ChangeProdCon("Producer produced "+value,prod,Color.GREEN,prod_panel));
    buffer[write]=value;
    write=(write+1)%buffer.length;
   // System.out.println(write);
    buffer_count++;
    notify();
  }
  public synchronized int get()
  {
    while(buffer_count==0)
    {
      try
      {
        SwingUtilities.invokeLater(new ChangeProdCon("Consumer can't consume "+"\n"+
                                                  "Consumer must wait.",con,Color.ORANGE,con_panel));
        wait();
      }
      catch(InterruptedException e){}
    }
    int readval=buffer[read];
    SwingUtilities.invokeLater(new ChangeProdCon("Consumer consumed "+readval,con,Color.GREEN,con_panel));
    read=(read+1)%buffer.length;
    buffer_count--;
    System.out.println(read);
    System.out.println(readval);
    notify();
    return readval;
  }
  private class ChangeProdCon implements Runnable
  {
    JLabel lbl;
    String s1;
    Color col;
    JPanel pnl;
    public ChangeProdCon(String s, JLabel l, Color c, JPanel p)
    {
      lbl=l;
      s1=s;
      col=c;
      pnl=p;
    }
    public void run()
    {
      lbl.setText(s1);
      pnl.setBackground(col);
    }
  }
  public void actionPerformed(ActionEvent e)
  {
    producer.start();
    consumer.start();
  }
  private class Runner implements Runnable
  {
    public void run()
    {
      for(int i=10; i<=500; i++)
      {
        try
        {
          Thread.sleep((int)(Math.random()*3001));
          set(i);
        }
        catch(InterruptedException e){}
      }
      SwingUtilities.invokeLater(new ChangeProdCon("Producer has finished producing.",prod,Color.RED,prod_panel));
    }
  }
  private class Runner2 implements Runnable
  {
              int i =0;
    public void run()
    {
      for( i=10; i<=500; i++)
      {
        try
        {
          Thread.sleep((int)(Math.random()*3001));
          sum=get();
        
          //sum+=get();
        }
        catch(InterruptedException e){}
        finally{
            //System.out.println(i);
        }
      }
      SwingUtilities.invokeLater(new ChangeProdCon("Consumer has finished consuming.",con,Color.RED,con_panel));
    }
  }
}

No comments:

Post a Comment