Last active
August 15, 2022 21:16
-
-
Save phoenix24/c23fb54af15a1061574cc4504d592638 to your computer and use it in GitHub Desktop.
sync experiment 6
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 { | |
Integer lock1 = 1; | |
public void testr() { | |
String name = Thread.currentThread().getName(); | |
synchronized(lock1) { | |
lock1 = lock1 + 1; | |
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.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