JavaThread           2011 4    9         twitter: @zaki50
Who am I•          YAMAZAKI Makoto(twitter: @zaki50)•   Android    •    •   StickyShortcut•           Java
• Effective Java       2  • ISBN:978-4-89471-499-1  • 3,780   (      )  •      66,       69
Agenda• synchronized / volatile• Lock(ReentrantLock)• java.util.concurrent
synchronized
Eclipse   (jvisualvm)
synchronizedclass Sample {    public synchronized void execute1() {        //    }    public void execute1b() {        syn...
synchronized•••
•
•         (by Wikipedia)• Java       1    1                 1                     1
•    • synchronized    • Object#wait()•    • synchronized    • Object#wait()
•    synchronized•
synchronizedclass Sample {    public synchronized void execute1() {        //    }    public void execute1b() {        syn...
•
•
• Java VM    •    ••
private static boolean stopRequested = false;private static int value = 0;public static void main(String[] args) throws Ex...
private static boolean stopRequested = false;private static int value = 0;public static void main(String[] args) throws Ex...
volatile
volatile•• volatile• volatile
volatileprivate volatile static boolean stopRequested = false;private volatile static int value = 0;public static void mai...
Lock
Lock• synchronized • lock() // synchronized • tryLock() // • tryLock(long,TimeUnit) // • lockInterruptibly() //
•    •           try - finally        Lock l = ...;        l.lock();        try {            //        } finally {         ...
ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();Lock wl = rwLock.writeLock();Lock rl;wl.lock();try {    // wr...
Lock• lock()     synchronized• unlock()     synchronized•                Lock
java.util.concurrent
...• wait()   notify()/notifyAll()                                  (    )•
java.util.concurrent•    Javadoc of java.util.concurrent    • ExecutorService    •    •    •    • ConcurrentCollections    •
• synchronized• Java           17.4 Memory Model•                       java.util.concurrent•     Java    • Doug Lea    IS...
ぐだ生 Java入門第ニ回(synchronized and lock)
Upcoming SlideShare
Loading in...5
×

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

1,318
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,318
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ぐだ生 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
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×