Skip to content

Instantly share code, notes, and snippets.

@fcracker79
Created January 3, 2020 07:46
Show Gist options
  • Save fcracker79/50829267e6594b979fdc445c76881ca4 to your computer and use it in GitHub Desktop.
Save fcracker79/50829267e6594b979fdc445c76881ca4 to your computer and use it in GitHub Desktop.
Two threads
package io.mirko;
public class Main {
static volatile boolean condition = true;
public static void main(String ... args) throws Exception {
new Thread(new AnotherThread()).start();
if (condition) {
methodA();
}
}
public static void methodA() {
synchronized(Main.class){
if (condition) {
condition = false;
System.out.println(Thread.currentThread());
} else {
Thread.currentThread().interrupt();
}
}
}
}
class AnotherThread implements Runnable{
@Override
public void run(){
if(Main.condition) {
Main.methodA();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment