-
-
Save mbakhoff/0fd51bfd653b778fcabc to your computer and use it in GitHub Desktop.
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class DeadlockDemo { | |
public static void main(String[] args) { | |
List<String> headLapsed = new ArrayList<>(); | |
List<String> pahadLapsed = new ArrayList<>(); | |
headLapsed.addAll(Arrays.asList( | |
"Karl", "Toom", "Rene", "Aadam", "Hans", "Henry", | |
"Madis", "Tiit", "Ott", "Simmo", "Aleksis", "Oskar", | |
"Heiti", "Veiko", "Dmitri", "Laura", "Gromet", "Stanislav")); | |
Thread märt = new Thread(() -> { | |
while (true) { | |
System.out.println(System.currentTimeMillis() + " kõik on pahad!"); | |
synchronized (headLapsed) { | |
synchronized (pahadLapsed) { | |
pahadLapsed.addAll(headLapsed); | |
headLapsed.clear(); | |
} | |
} | |
} | |
}, "Märt"); | |
Thread taavi = new Thread(() -> { | |
while (true) { | |
System.out.println(System.currentTimeMillis() + " kõik on head!"); | |
synchronized (pahadLapsed) { | |
synchronized (headLapsed) { | |
headLapsed.addAll(pahadLapsed); | |
pahadLapsed.clear(); | |
} | |
} | |
} | |
}, "Taavi"); | |
märt.start(); | |
taavi.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment