Skip to content

Instantly share code, notes, and snippets.

@jtuttas
Created September 14, 2014 13:43
Show Gist options
  • Save jtuttas/6ab4b8af1fc7e73d246a to your computer and use it in GitHub Desktop.
Save jtuttas/6ab4b8af1fc7e73d246a to your computer and use it in GitHub Desktop.
Threads Fork and Join
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Zaehler z1 = new Zaehler("Nummer 1", 3);
Zaehler z2 = new Zaehler("Nummer 2", 6);
z1.start();
z2.start();
while (z1.isAlive() && z2.isAlive()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Zählen beendet!");
}
}
public class Zaehler extends Thread {
private int zahl;
public Zaehler (String n, int z) {
super(n);
zahl=z;
}
public void run() {
for (int i=0;i<zahl;i++) {
System.out.println ("Thread "+getName()+": ist bei "+i);
try {
this.sleep(100);
}
catch (InterruptedException ix) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment