Skip to content

Instantly share code, notes, and snippets.

@ichistmeinname
Last active August 29, 2015 14:01
Show Gist options
  • Save ichistmeinname/8924204386e21147cb7b to your computer and use it in GitHub Desktop.
Save ichistmeinname/8924204386e21147cb7b to your computer and use it in GitHub Desktop.
Third group session for advanced programming.
public class Counter extends Thread {
private String name;
private int count;
private int delay;
public Counter(String name, int delay) {
this.name = name;
this.delay = delay;
this.count = 0;
}
public void run() {
while (true) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {}
count++;
System.out.println(name + ": " + count);
}
}
public static void main(String[] args) {
int numCounters = 0;
for (String arg : args) {
// create counter
// Counter c = new Counter("Counter"+(numCounters++),Integer.parseInt(arg));
// c.start();
// kuerzer
new Counter("Counter"+(numCounters++),Integer.parseInt(arg)).start();
}
}
}
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class CounterGUI extends WindowAdapter implements Runnable {
private Integer counter = 0;
private long delay;
private boolean running;
private JFrame frame;
private JLabel label;
public CounterGUI(String name, long delay) {
this.delay = delay;
this.running = true;
frame = new JFrame(name);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(this);
label = new JLabel("0000", SwingConstants.CENTER);
label.setFont(new Font(null, Font.BOLD, 100));
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public void windowClosed(WindowEvent e) {
running = false;
}
public void run() {
while (true) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {}
counter++;
label.setText(counter.toString());
}
}
public static void main(String[] args) {
int numCounters = 0;
for (String arg : args) {
CounterGUI c = new CounterGUI("Counter"+(numCounters++),Integer.parseInt(arg));
Thread t = new Thread(c);
t.start();
}
}
}
public class PingPong {
private boolean pingsTurn; // true = ping, false = pong is allow to play
private String output;
public PingPong() {
this.pingsTurn = true;
}
public synchronized void ping() {
while(!pingsTurn) {
try {
wait();
} catch (InterruptedException e) {}
}
output = new String("ping");
System.out.println(output);
pingsTurn = false;
notify();
}
public synchronized void pong() {
while (pingsTurn) {
try {
wait();
} catch (InterruptedException e) {}
}
output = new String("pong");
System.out.println(output);
pingsTurn = true;
notify();
}
public static void main (String[] args) {
PingPong game = new PingPong();
Ping p1 = new Ping(game);
Pong p2 = new Pong(game);
p1.start();
p2.start();
}
private static class Ping extends Thread {
private PingPong game;
public Ping(PingPong game) {
this.game = game;
}
public void run() {
while (true) {
game.ping();
}
}
}
private static class Pong extends Thread {
private PingPong game;
public Pong(PingPong game) {
this.game = game;
}
public void run() {
while (true) {
game.pong();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment