Learning Java 3 – Threads and Synchronization

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Learning Java 3 – Threads and Synchronization - Presentation Transcript

    1. Learning Java 3 –Threads, Synchronization Christopher Swenson Center for Information Security University of Tulsa 600 S. College Ave Tulsa, OK 74104
    2. Overview
      • Threads
      • Synchronization of Threads
    3. Threads
      • Everything up to now has been single processing: only one thing was executing at once
      • Threads allow multiple things to execute simultaneously
      • Most of what you need is in the Thread class
    4. How to Make a Thread
      • You need to make a public void run() function
      • Two options
        • Class that implements Runnable
        • Class that extends Thread
          • Anonymous inner class
      • t = new Thread( Runnable r);
      • t = new MyThread();
    5. Starting/Stopping
      • Say we have a Thread t
      • t.start() – starts executing the Thread’s run()
      • t.interrupt() – interrupts a Thread
      • t.join() – waits for Thread to die
    6. Example
      • Thread t1 = new Thread(task1);
      • Thread t2 = new Thread(task2);
      • t1.start(); t2.start();
      • t1.join(); t2.join();
    7. Useful Thread Methods
      • Thead.sleep( long millis[, int nanos]);
        • Causes current executing Thread to sleep
      • Thread.yield();
        • Causes current executing Thread to be put back on the ready list
    8. Anonymous Inner Classes
      • Quick hack to start a new Thread
      • Inner classes are bound to an instance of the enclosing class (so can access its variables)
      • Be careful with them
      • Thread t1 = new Thread() {
      • public void run() {
      • // Do stuff
      • } };
      • t1.start();
    9. Clobbering Time!
      • So now we have multiple Threads going at once, but they can trample each other’s feet
        • Writing to the same variable at the same time
        • Reading and writing at the same time where order matters
      • Some objects are inherently thread-safe (or “synchronized”)
        • Vector, Hashtable, Collections.synchronizedList(List l)
      • Synchronization is the key
    10. Locks
      • Locks are the primitive for doing synchronization in Java
      • Every Object is a lock
      • Two methods that have code that is synchronized on the same lock (object) cannot execute that code at the same time
    11. Example
      • ArrayList<Integer> list = new ArrayList<Integer>();
      • //…
      • synchronized (list)
      • {
      • list.add(4);
      • }
    12. Synchronized methods
      • If an method is synchronized, then the entire code is wrapped in an implicit synchronized (this) { … }
      • They will all be synchronized to their instance
      • Only one synchronized method of an instance of an object can be running at one time
    13. Waiting, Notifying
      • Well, what if I am waiting for something, but it may be a while?
      • Must hold the lock of the Object you are waiting / notifying on (which will be released and reacquired as necessary)
      • Object bufferFull = new Object();
      • //… the rest should be synchronized on buffer
      • while (buffer.size() == 10) arrayFull.wait();
      • //…
      • // notify one thread
      • buffer.remove();
      • arrayFull.notify();
      • // or notify everyone!
      • arrayFull.notifyAll();
    14. Advanced Synchronization
      • Java 1.5 adds many shiny new synchronization mechanisms (java.util.concurrent.*)
      • Lock provides non-blocking access to code
        • ReentrantLock
      • Condition provides an implementation of condition variables
        • Associated with a Lock
      • Semaphore
      • AtomicInteger , etc

    + caswensoncaswenson, 6 months ago

    custom

    1240 views, 0 favs, 1 embeds more stats

    Basic introduction to threads and synchronization i more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1240
      • 1239 on SlideShare
      • 1 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 49
    Most viewed embeds
    • 1 views on http://static.slideshare.net

    more

    All embeds
    • 1 views on http://static.slideshare.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories