Created
January 3, 2020 07:46
-
-
Save fcracker79/50829267e6594b979fdc445c76881ca4 to your computer and use it in GitHub Desktop.
Two 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
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