Skip to content

Instantly share code, notes, and snippets.

@ge0ffrey
Created September 1, 2016 15:16
Show Gist options
  • Save ge0ffrey/73e83b25c3a9a4632efaa5b602640994 to your computer and use it in GitHub Desktop.
Save ge0ffrey/73e83b25c3a9a4632efaa5b602640994 to your computer and use it in GitHub Desktop.
ublic class TwMain {
private final static Semaphore SEMAPHORE = new Semaphore(2, true);
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
StringBuilder name = new StringBuilder();
for (int j = 0; j < i + 1; j++) {
name.append(i);
}
Thread thread = new Thread(new Runner(name.toString()));
thread.start();
}
}
public static class Runner implements Runnable {
private final String name;
private long count = 0;
public Runner(String name) {
this.name = name;
}
@Override
public void run() {
while (count < 3000) {
if (count % 100 == 0) {
if (count != 0) {
SEMAPHORE.release();
}
try {
SEMAPHORE.acquire();
} catch (InterruptedException e) {
e.printStackTrace(); // TODO generated
}
System.out.println((System.currentTimeMillis() % 100000) + " - " + name + " = " + count);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
count++;
}
System.out.println((System.currentTimeMillis() % 10000) + " - " + name + " finished.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment