Created
January 17, 2019 13:12
-
-
Save phoenix24/7ca447df191abec8ce747e30ffd7c9df to your computer and use it in GitHub Desktop.
sync experiment 7
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 SyncTest { | |
volatile Boolean lock1 = true; | |
public void testr() { | |
String name = Thread.currentThread().getName(); | |
synchronized(lock1) { | |
lock1 = !lock1; | |
long time1 = System.currentTimeMillis(); | |
System.out.println("bef: name:" + name + ", time: " + time1 + ", lock1:" + lock1); | |
try { | |
Thread.sleep(10000); | |
} catch (InterruptedException e) { e.printStackTrace(); } | |
long time2 = System.currentTimeMillis(); | |
System.out.println("aft: name:" + name + ", time: " + time2 + ", lock1:" + lock1); | |
} | |
} | |
public static void main(String[] args) { | |
SyncTest st1 = new SyncTest(); | |
ExecutorService ex = Executors.newFixedThreadPool(3); | |
ex.submit(st1::testr); | |
ex.submit(st1::testr); | |
ex.submit(st1::testr); | |
ex.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment