Last active
April 5, 2023 12:57
-
-
Save githiago-f/ef0e24ffd6405ca4c4bc62610d04a443 to your computer and use it in GitHub Desktop.
testing threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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