Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ぐだ生 Java入門第ニ回(synchronized and lock)

1,498 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ぐだ生 Java入門第ニ回(synchronized and lock)

  1. 1. JavaThread 2011 4 9 twitter: @zaki50
  2. 2. Who am I• YAMAZAKI Makoto(twitter: @zaki50)• Android • • StickyShortcut• Java
  3. 3. • Effective Java 2 • ISBN:978-4-89471-499-1 • 3,780 ( ) • 66, 69
  4. 4. Agenda• synchronized / volatile• Lock(ReentrantLock)• java.util.concurrent
  5. 5. synchronized
  6. 6. Eclipse (jvisualvm)
  7. 7. synchronizedclass Sample { public synchronized void execute1() { // } public void execute1b() { synchronized (this) { // } } public static synchronized void execute2() { // } public static synchronized void execute2b() { synchronized (Sample.class) { // } }}
  8. 8. synchronized•••
  9. 9.
  10. 10. • (by Wikipedia)• Java 1 1 1 1
  11. 11. • • synchronized • Object#wait()• • synchronized • Object#wait()
  12. 12. • synchronized•
  13. 13. synchronizedclass Sample { public synchronized void execute1() { // } public void execute1b() { synchronized (this) { // } } public static synchronized void execute2() { // } public static synchronized void execute2b() { synchronized (Sample.class) { // } }}
  14. 14.
  15. 15.
  16. 16. • Java VM • ••
  17. 17. private static boolean stopRequested = false;private static int value = 0;public static void main(String[] args) throws Exception { new Thread() { @Override public void run() { int i = 0; if (!stopRequested) { while (!stopRequested) { while (true) { i++; i++; } } } System.out.println(value); } }.start(); TimeUnit.SECONDS.sleep(1L); value = 100; stopRequested = true;}
  18. 18. private static boolean stopRequested = false;private static int value = 0;public static void main(String[] args) throws Exception { new Thread() { @Override public void run() { int i = 0; while (!stopped()) { i++; } System.out.println(value); } }.start(); private synchronized static void stop() { stopRequested = true; TimeUnit.SECONDS.sleep(1L); } value = 100; stop(); private synchronized static boolean stopped() {} return stopRequested; }
  19. 19. volatile
  20. 20. volatile•• volatile• volatile
  21. 21. volatileprivate volatile static boolean stopRequested = false;private volatile static int value = 0;public static void main(String[] args) throws Exception { new Thread() { @Override public void run() { int i = 0; while (!stopRequested) { i++; } System.out.println(value); } }.start(); TimeUnit.SECONDS.sleep(1L); value = 100; stopRequested = true;}
  22. 22. Lock
  23. 23. Lock• synchronized • lock() // synchronized • tryLock() // • tryLock(long,TimeUnit) // • lockInterruptibly() //
  24. 24. • • try - finally Lock l = ...; l.lock(); try { // } finally { l.unlock(); }
  25. 25. ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();Lock wl = rwLock.writeLock();Lock rl;wl.lock();try { // write rl = rwLock.readLock();} finally { wl.unlock();}try { // read} finally { rl.unlock();}
  26. 26. Lock• lock() synchronized• unlock() synchronized• Lock
  27. 27. java.util.concurrent
  28. 28. ...• wait() notify()/notifyAll() ( )•
  29. 29. java.util.concurrent• Javadoc of java.util.concurrent • ExecutorService • • • • ConcurrentCollections •
  30. 30. • synchronized• Java 17.4 Memory Model• java.util.concurrent• Java • Doug Lea ISBN:4-7973-3720-6

×