Skip to content

Instantly share code, notes, and snippets.

@githiago-f
Last active April 5, 2023 12:57
Show Gist options
  • Save githiago-f/ef0e24ffd6405ca4c4bc62610d04a443 to your computer and use it in GitHub Desktop.
Save githiago-f/ef0e24ffd6405ca4c4bc62610d04a443 to your computer and use it in GitHub Desktop.
testing threads
public class RuPaulThreadsRace {
static boolean ready = false;
static volatile StringBuffer stringBuffer = new StringBuffer();
private static abstract class Tester extends Thread {
abstract String name();
void recursive(int i) {
if(i==0){ return; }
stringBuffer.append(name());
recursive(--i);
}
@Override
public void run() {
while(!ready) { Thread.yield(); }
recursive(100);
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("In your positions!");
new Tester() {
String name() {
return "A";
}
}.start();
new Tester() {
String name() {
return "B";
}
}.start();
System.out.println("ready?!");
Thread.sleep(10000L);
System.out.println("GO!");
ready = true;
Thread.sleep(1000L);
System.out.println("Race result: ");
System.out.println(stringBuffer.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment