Skip to content

Instantly share code, notes, and snippets.

@jacbar
Created January 8, 2011 12:29
Show Gist options
  • Save jacbar/770796 to your computer and use it in GitHub Desktop.
Save jacbar/770796 to your computer and use it in GitHub Desktop.
import java.io.*;
import javax.swing.*;
class Bufor {
static int buforSize = 1;
static int actualSize = 0;
private JTextArea text = null;
Bufor(JTextArea text) {
this.text = text;
}
synchronized public void addProduct(ObjectOutputStream out, String name)
throws IOException {
String message = "Chce oddac #\n";
out.writeObject(message);
text.append(name +" chce oddac #\n");
while (actualSize >= buforSize) {
try {
message = "Bufor pelny - czekam\n";
out.writeObject(message);
text.append(name +" bufor pelny - czeka\n");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
actualSize++;
message = "Oddalem #\n";
out.writeObject(message);
text.append(name+" oddal #\n");
notifyAll();
}
synchronized public void getProduct(ObjectOutputStream out, String name)
throws IOException {
String message = "Chce zabrac #\n";
text.append(name+" chce zabrac #\n");
out.writeObject(message);
while (actualSize == 0) {
try {
message = "Bufor pusty - czekam\n";
out.writeObject(message);
text.append(name+" bufor pusty - czeka\n");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
actualSize--;
message = "Zabralem #\n";
out.writeObject(message);
text.append(name+" zabral #\n");
notifyAll();
}
synchronized static void setSize(int a) {
buforSize = a;
if (actualSize > buforSize)
actualSize = buforSize;
// JOptionPane.showMessageDialog(null, buforSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment