Skip to content

Instantly share code, notes, and snippets.

@phoenix24
Last active August 15, 2022 21:16
Show Gist options
  • Save phoenix24/c23fb54af15a1061574cc4504d592638 to your computer and use it in GitHub Desktop.
Save phoenix24/c23fb54af15a1061574cc4504d592638 to your computer and use it in GitHub Desktop.
sync experiment 6
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