Created
January 17, 2019 13:09
-
-
Save phoenix24/2114f6c096f906485cfe8a9a79d282df to your computer and use it in GitHub Desktop.
sync experiment 4
This file contains hidden or 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 { | |
| Boolean lock1 = true; | |
| public void testr() { | |
| String name = Thread.currentThread().getName(); | |
| synchronized(lock1) { | |
| long time1 = System.currentTimeMillis(); | |
| System.out.println("bef: name:" + name + ", time: " + time1 + ", lock1:" + lock1); | |
| try { | |
| Thread.sleep(10000); | |
| lock1 = !lock1; | |
| } 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