Producer-Consumer Problem

Producer-Consumer Problem using Semaphore

Producer() {

put(item) {

P(empty); // for wait, Get an empty count, block if unavailable
P(mutex); // lock critical section: shared buffer
<<< critical section: Put item into shared buffer >>>
V(mutex); // unlock critical section
V(full); // for signal, increase number of full count

}

}

Consumer() {

take () {

P(full); // for wait, Get a full count, block if unavailable
P(mutex);
<<< critical section: Take item from shared buffer >>>
V(mutex);
V(empty); // for signal, increase number of empty count
return item;

}

}

Producer-Consumer Problem using Monitor

Producer() {

synchronized put(item) {

while(count >= buffer.length) { wait(); } // wait if buffer is full
count++
<<< critical section: Put iteminto shared buffer >>>
notifyAll(); // signal

}

}

Consumer() {

synchronized take () {

while(count <=0) { wait(); } // wait if buffer is empty
count- –
<<< critical section: Take itemfrom shared buffer >>>
notifyAll(); // signal
return item;

}

}

Concurrency in Swing

Concurrency in Swing
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

A Swing programmer deals with the following kinds of threads:

 

Java Thread Synchronization

Thread Synchronization

https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

  • Mutex Semaphore (aka Mutex) controls only one thread at a time executing on the shared resource by lock & unlock.
  • Counting Semaphore (aka Semaphore – Java7) controls the number of threads executing on the shared resource by acquire & release.
  • Monitor controls only one thread at a time, and can execute in the monitor (shared object) by wait & notify/notifyAll.

lab6

Lab6 (updated)

java2-lab6-Thread

Lab6_1 extends Thread vs implements Runnable

Lab6_2 Mutex (Mutual Exclusion Semaphore)

Lab6_3 Semaphore (e.g. TicketSeller & Producer-Consumer Problem)

Lab6_4 Monitor (e.g. SynchronizedCounter & Producer-Consumer Problem)

Lab6_5 SwingWorker (e.g.

Lab6_6 EventDispatchThread

ChangeListener, KeyListener, ActionListener

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainFrame extends JFrame implements ChangeListener, KeyListener, ActionListener {

JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel(new FlowLayout());
 JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);
 JTextField textfield = new JTextField(10);
 JButton button = new JButton(“Close”);

public MainFrame(String name) {
super(name);

this.add(panel);
panel1.add(label);
panel1.add(textfield);
panel.add(slider, BorderLayout.CENTER);
panel.add(panel1, BorderLayout.NORTH);
panel.add(button, BorderLayout.SOUTH);

slider.addChangeListener(this); // ChangeListener stateChanged
textfield.addKeyListener(this); // KeyListener keyTyped, keyPressed, keyReleased
button.addActionListener(this); // ActionListener actionPerformed

this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_2”);
}

@Override
 public void stateChanged(ChangeEvent e) {
  JSlider s = (JSlider)e.getSource();
  label.setText(“Value : ” + s.getValue()); 
 }

@Override
 public void keyTyped(KeyEvent e) { 
 }

 @Override
 public void keyPressed(KeyEvent e) {
 }

 @Override
 public void keyReleased(KeyEvent e) {
     int key = e.getKeyCode();
     if (key == KeyEvent.VK_ENTER) {
      Toolkit.getDefaultToolkit().beep();
      System.out.println(“ENTER pressed”);
      JTextField t = (JTextField) e.getSource();
     int value = Integer.parseUnsignedInt(t.getText());
     slider.setValue(value);
     }
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  JButton b = (JButton)e.getSource();
  if (b == button) {
   this.setVisible(false);
   this.dispose();
  }
 }

}

Event Listener

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainFrame extends JFrame {

JPanel panel = new JPanel(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);

public MainFrame(String name) {
super(name);

this.add(panel);
panel.add(slider, BorderLayout.CENTER);
panel.add(label, BorderLayout.SOUTH);

// using anonymous class

slider.addChangeListener(new ChangeListener() { 
  @Override
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText(“Value : ” + s.getValue());
}
});

this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest_5_1_1”);
}

}


 

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// using implements ChangeListener

public class MainFrame extends JFrame implements ChangeListener {

JPanel panel = new JPanel(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);

public MainFrame(String name) {
super(name);

this.add(panel);
panel.add(slider, BorderLayout.CENTER);
panel.add(label, BorderLayout.SOUTH);

slider.addChangeListener(this); // implements ChangeListener

this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_2”);
}

@Override
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText(“Value : ” + s.getValue());
}
}


import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainFrame extends JFrame {

JPanel panel = new JPanel(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);

// using private inner class

private class SliderChangeListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
label.setText(“Value : ” + s.getValue());
}
}

public MainFrame(String name) {
super(name);

this.add(panel);
panel.add(slider, BorderLayout.CENTER);
panel.add(label, BorderLayout.SOUTH);

slider.addChangeListener(new SliderChangeListener()); // private class

this.setSize(250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_3”);
}
}