Skip to content

Instantly share code, notes, and snippets.

@phoenix24
Created January 17, 2019 13:12
Show Gist options
  • Save phoenix24/7ca447df191abec8ce747e30ffd7c9df to your computer and use it in GitHub Desktop.
Save phoenix24/7ca447df191abec8ce747e30ffd7c9df to your computer and use it in GitHub Desktop.
sync experiment 7
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