Skip to content

Instantly share code, notes, and snippets.

@phoenix24
Created January 17, 2019 13:04
Show Gist options
  • Select an option

  • Save phoenix24/53905dff87d78ee0caa37b868ff57cfc to your computer and use it in GitHub Desktop.

Select an option

Save phoenix24/53905dff87d78ee0caa37b868ff57cfc to your computer and use it in GitHub Desktop.
sync experiment 3
public class SyncTest {
Character lock1 = 2;
//Character lock1 = 200; //vs using this for lock.
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);
} 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();
SyncTest st2 = new SyncTest();
SyncTest st3 = new SyncTest();
ExecutorService ex = Executors.newFixedThreadPool(3);
ex.submit(st1::testr);
ex.submit(st2::testr);
ex.submit(st3::testr);
ex.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment