ABHRA BASAK
APOORVA KUMAR
What will be covered?
• Concurrency and Parallelism in the Java Runtime Environment
• The Java Thread Model - Decisions for Design
• Implementin...
The Java Runtime
Environment
• Allows you to run the Java Compiler at the same time that
you are using an editor
Process-based multitasking
• A text ed...
Java
Libraries
Java
Code
Byte code
Byte code
Java Virtual Machine
Executable
Instructions
Decisions for Design
• Thread priorities are integers assigned by the Java runtime that
specify the relative priority of one thread to another....
• An elegant twist to an age-old model of inter-process
synchronization – the monitor.
• Most multithreaded systems expose...
Thread 1 Thread 2
Creating and Managing
Threads
Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
• Thread execution
• start() – Start a thread by calling its run() method
• run() – Entry point for the thread
• Thread bl...
• Thread lifetime and termination
• isAlive() – Determine if a thread is still running
• join() – Wait for a thread to ter...
Remote Method Invocation
• The RMI facility allows you to invoke operations on objects running in
other JVMs.
• When a remote method is called, the...
The Concurrent API
Packages
• Concurrency APIs
java.util.concurrent
• Atomic data types
java.util.concurrent.atomic
• Locks for synchronization
java.u...
• Implements the classic semaphore
Semaphore
• Waits until a specified number of events have occurred
CountDownLatch
• Ena...
Executor
ExecutorService
ThreadPool
Executor
ScheduledPool
Executor
Future
Callable
Concurrent
HashMap
Concurrent
LinkedQueue
CopyOnWrite
ArrayList
Array
BlockingQueue
Concurrent
SkipListMap
Concurrent
Skip...
TimeUnit
Enumeration
DAYS
HOURS
MINUTES
SECONDS
MICRO
SECONDS
MILLI
SECONDS
NANO
SECONDS
Lock
ReentrantLock
ReadWriteLock
Reentrant
ReadWriteLock
AtomicInteger
AtomicLong
decrementAndGet() compareAndSet()
getAndSet()
Sharing Access to Data
Thread
safe
Behaves
correctly
(unambiguous)
Independent of
scheduling and
interleaving
Without
additional
synchronization
synchronized
• The primary mechanism for
synchronization
volatile
• Enables the thread to bypass the cache
when accessing ...
Multithreaded GUIs
• GUI systems use a single thread called the Event Dispatch Thread for
handling GUI events
• Multithreaded GUIs tend to be...
What we looked at
• Concurrency and Parallelism in the Java Runtime Environment
• The Java Thread Model - Decisions for Design
• Implementin...
• Android – A Java based mobile application platform
• Java Swing, AWT and Applets
• Process scheduling in Java Operating ...
Time for Questions and
Comments
• Tim Peierls, Brian Goetz, Joshua Bloch, Joseph Bowbeer, Doug Lea, and
David Holmes. 2005. Java Concurrency in Practice. ...
• Alan D. Fekete. 2008. Teaching students to develop thread-safe java classes.
In Proceedings of the 13th annual conferenc...
Concurrency in java
Concurrency in java
Upcoming SlideShare
Loading in...5
×

Concurrency in java

616

Published on

Published in: Technology, News & Politics
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
616
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • Multitasking – Thread-based and Process-basedProcess-based multitasking – Allows you to run the Java Compiler at the same time that you are using an editorThread-based multitasking – A text editor can format text at the same time that it is printing
  • The Java Code and the Java libraries are together compiled to byte code.
  • The byte code is then interpreted in the Java Virtual Machine (JVM) as executable instructions.Unlike many computer languages, Java provides built-in support for multithreading.All Java programs are multi-threaded (Inherent concurrency)The Garbage Collector always runs as a separate thread in the background.The Java runtime system depends upon threads for many things, and all the class libraries are designed with multithreading in mind.
  • Single Threaded systems use an approach called an event loop with polling.In this model, a single thread of control runs in an infinite loop, polling a single event queue to decide what to do next.Until an event handler returns, nothing else can happen in the system. This wastes CPU time.It can also result in one part of a program dominating the system, and preventing any other events from being processed.When a thread blocks, i.e., suspends execution, because it is waiting for some other resource, the entire program stops running.The benefit of Java’s multithreading is that the main loop / polling mechanism is eliminated.When a thread blocks in a Java program, only the single thread that is blocked pauses. All other threads continue to run.
  • As an absolute value, a priority is meaningless. A higher priority thread doesn’t run any faster than a lower priority thread if it is the only thread running.Instead a thread’s priority is used to decide when to switch from one thread to the next. This is called a context switch.Relinquish: This is done by explicitly yielding, sleeping, or blocking on pending I/O. In this scenario, all other threads are examined, and the highest priority thread that is ready to run is given the CPU.Pre-emption: In this case a lower priority thread that does not yield the processor is simply preempted – no matter what it is doing – by a higher priority thread. Basically, as soon as a higher priority thread wants to run, it does. This is called preemptive multitasking.
  • If you want two threads to communicate and share a complicated data structure, you need some way to ensure that they don’t conflict with each other.You can think of a monitor as a very small box that can hold only one thread. Once a thread enters a monitor, all other threads must wait until the thread exits the monitor.In this way, the monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.Once a thread is inside a synchronised method, no other thread can call any other synchronised methods on the same object. This helps you write very clear and concise multithreaded code, because synchronisation support is built into the language.
  • If you want two threads to communicate and share a complicated data structure, you need some way to ensure that they don’t conflict with each other.You can think of a monitor as a very small box that can hold only one thread. Once a thread enters a monitor, all other threads must wait until the thread exits the monitor.In this way, the monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.Once a thread is inside a synchronised method, no other thread can call any other synchronised methods on the same object. This helps you write very clear and concise multithreaded code, because synchronisation support is built into the language.
  • The program needs to define how separate threads communicate with each other.Java provides a clean, low-cost way for two or more threads to talk to each other, via calls to predefined methods that all objects have.Java’s messaging system allows a thread to enter a synchronized method on an object, and then wait there until another thread explicitly notifies it to come out.
  • Create an instance of a class that extends class Thread.This class must override the run() method, which is the entry point of the new thread.It must also call start() to begin execution of the new thread.
  • Create a class that implements the interface Runnable.An object of class Thread is instantiated from within the class.The class should override the method run() which is the entry point for the new thread.
  • Inside run(), the code constitutes the new thread. run() establishes the entry point for another concurrent thread of execution within the program. Thus thread will end when run() returns.After the new thread is created, it will not start running until its start() method is called, which is declared within Thread. In essence, start() executes a call to run().A thread can be paused / suspended for a period of time using sleep(). Another thread can interrupt a sleeping thread. The sleeping thread then throws an InterruptedException that has to be caught explicitly by the program.
  • The byte code is then interpreted in the Java Virtual Machine (JVM) as executable instructions.
  • As conceptually pure as Java’s original support for multithreading is, it is not ideal for all applications – especially those that make extensive use of multiple threads. For example the original multithreading support does not provide several high-level features, such as semaphores, thread pools, and execution managers, that facilitate the creation of intensive concurrent programs.JDK 5 added the concurrency utilities, also commonly referred to as the concurrent API.The concurrency utilities are contained in the java.util.concurrent package and its subpackages java.util.concurrent.atomic and java.util.concurrent.locksjava.util.concurrent defines the core features that support alternatives to the built-in approaches to synchronization and interthread communication.
  • Offer high level ways of synchronizing the interactions between multiple threads. Collectively, they enable one to handle several formerly difficult synchronization situations with ease. Each synchronizer provides a solution to a specific type of synchronization problem. This enables each synchronizer to be optimized for its intended use.A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than 0, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource.Sometimes you will want a thread to wait until one or more events have occurred. A CountDownLatch is initially created with a count of the number of events that must occur before the latch is released.When a set of two or more threads must wait at a predetermined execution point until all threads in the set have reached that point, to handle such a situation the API supplies a CyclicBarrier class. It enables you to define a synchronization object that suspends until the specified number of threads has reached the barrier point.Most interesting of the synchronizer classes is the Exchanger. It is designed to simplify the exchange of data between two threads. The operation of an Exchanger is astoundingly simple. It simply waits until two separate threads call its exchange() method. When that occurs, it exchanges the data supplied by the threads. This mechanism is both elegant and easy to use. For example, one thread might prepare a buffer for receiving information over a network connection, another might fill that buffer with the information from the connection.
  • They manage thread execution. At the top of the Executor hierarchy is the Executor interface, which is used to initiate a thread. It defines execute().The ExecutorService extends Executor and provides methods that manage execution. There are two implemetations of ExecutorService : ThreadPoolExecutor and ScheduledPoolExecutor. Related to Executors are Future and Callable interfaces. Callable defines a thread that defines a value. An application can use Callable objects to compute results that are then returned to the invoking thread. This is a powerful mechanism because it facilitates the coding of numerical computations in which partial reults are computed simultaneously. It could also be used to return status codes to indicate successful completion of threads. A callable task is executed by the ExecutorService, by calling its submit() method.A Future contains a value that is returned by a thread after it executes. Thus its value becomes defined in the future, when the thread terminates. It is a generic interface that represents a value that will be returned by a Callable object.
  • The package defines several concurrent collection classes such as ConcurrentHashMap,ConcurrentLinkedQueue, CopyOnWriteArrayList. These offer concurrent alternatives to their related classes defined by the collections framework.
  • TimeUnit is an enumeration that is used to specify the granularity of the timing. The arguments of this type generally indicate a timeout period.Caution: there is no guarantee that the system is capable of the specified resolution, even if any of these values is allowed to be specified.
  • The java.util.concurrent.locks package provides support for locks, which are objects that offer an alternative to using “synchronized” to control access to a shared resource. Before accessing a shared resource, the lock that protects the resource is acquired. When access to the resource is complete, the lock is released. If a second thread attempts to acquire the lock when it is in use by another thread, the second thread will suspend until the lock is released.All locks implement the Lock interface. The methods defined by Lock are:lock(), unlock(), lockInterruptibly() – waits until the invoking lock can be acquired unless interrupted, newCondition() – returns a Condition object that is associated with the invoking lock, tryLock() – attempts to acquire the lock, if unavailable, it will not wait, or will wait for no longer than the period specified.ReentrantLock implements Lock. It is a lock that can be repeatedly entered by the thread that currently holds the lock. (There should be an equal number of calls to unlock() to offset all calls to lock().) Otherwise, a thread seeking to acquire the lock will suspend until the lock is not in use.
  • The java.util.concurrent.atomic package offers an alternative to the other synchronization features when reading or writing the values of some types of variables. This package offers methods that get, set, or compare the value of a variable in one uninterruptible atomic operation. This means that no lock or other synchronization mechanism is required.In general, the atomic operations offer a convenient, and possibly more efficient, alternative to the other synchronization mechanisms when only a single variable is involved.
  • As classes and objects can be accessed by multiple threads when shared between them we need to ensure they are thread-safe.When is a class thread safe?It behaves correctly(ambiguous)Independent of thread scheduling and interleavingWithout additional synchronization
  • Synchronized keyword: The primary mechanism for synchronization.Every object has an intrinsic lock or monitor associated with it. A thread needing exclusive and consistent access has to acquire the intrinsic lock.Every class also has such a lock associated with it for synchronizing access to static methods.Critical Sections of a program are marked with the synchronized keyword. Particularly useful for fine grained concurrencyVolatile keyword: This keyword enables the thread to bypass the cache when accessing the data.
  • Concurrency in java

    1. 1. ABHRA BASAK APOORVA KUMAR
    2. 2. What will be covered?
    3. 3. • Concurrency and Parallelism in the Java Runtime Environment • The Java Thread Model - Decisions for Design • Implementing Multithreading – Creating and Managing Threads • Remote Method Execution – Java RMI • The Concurrency Utilities – java.util.concurrent • Thread Safety – Shared Access to Data • GUI Applications in Java
    4. 4. The Java Runtime Environment
    5. 5. • Allows you to run the Java Compiler at the same time that you are using an editor Process-based multitasking • A text editor can format text at the same time that it is printing Thread-based multitasking
    6. 6. Java Libraries Java Code Byte code
    7. 7. Byte code Java Virtual Machine Executable Instructions
    8. 8. Decisions for Design
    9. 9. • Thread priorities are integers assigned by the Java runtime that specify the relative priority of one thread to another. • A thread can voluntarily relinquish control. • A thread can be preempted by a higher priority thread.
    10. 10. • An elegant twist to an age-old model of inter-process synchronization – the monitor. • Most multithreaded systems expose monitors as objects that the program must explicitly acquire and manipulate. • Java provides a cleaner solution : each object has its own implicit monitor, automatically called while invoking synchronized methods.
    11. 11. Thread 1 Thread 2
    12. 12. Creating and Managing Threads
    13. 13. Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
    14. 14. Source : Parallel Programming for Multicore and Cluster Systems - Rauber & Runger
    15. 15. • Thread execution • start() – Start a thread by calling its run() method • run() – Entry point for the thread • Thread blocking • sleep() – Suspend a thread for a period of time • yield() – Voluntarily relinquish thread control to another thread of the same priority
    16. 16. • Thread lifetime and termination • isAlive() – Determine if a thread is still running • join() – Wait for a thread to terminate • Thread communication • wait() – Instructs the calling thread to give up the monitor and sleep until some other thread enters the same monitor and calls notify() • notify() – Wakes up a thread that called wait() on the same object • notifyAll() – Wakes up all threads that called wait() on the same object
    17. 17. Remote Method Invocation
    18. 18. • The RMI facility allows you to invoke operations on objects running in other JVMs. • When a remote method is called, the RMI stub, created by the RMI compiler, packages the method parameters and sends them over the network. • The remote system unpacks the arguments and then calls the remote method.
    19. 19. The Concurrent API Packages
    20. 20. • Concurrency APIs java.util.concurrent • Atomic data types java.util.concurrent.atomic • Locks for synchronization java.util.concurrent.locks
    21. 21. • Implements the classic semaphore Semaphore • Waits until a specified number of events have occurred CountDownLatch • Enables a group of threads to wait at a predefined execution point CyclicBarrier • Exchanges data between two threads Exchanger
    22. 22. Executor ExecutorService ThreadPool Executor ScheduledPool Executor Future Callable
    23. 23. Concurrent HashMap Concurrent LinkedQueue CopyOnWrite ArrayList Array BlockingQueue Concurrent SkipListMap Concurrent SkipListSet CopyOnWrite ArraySet DelayQueue LinkedBlocking Deque LinkedBlocking Queue Prioirty BlockingQueue Synchronous Queue
    24. 24. TimeUnit Enumeration DAYS HOURS MINUTES SECONDS MICRO SECONDS MILLI SECONDS NANO SECONDS
    25. 25. Lock ReentrantLock ReadWriteLock Reentrant ReadWriteLock
    26. 26. AtomicInteger AtomicLong decrementAndGet() compareAndSet() getAndSet()
    27. 27. Sharing Access to Data
    28. 28. Thread safe Behaves correctly (unambiguous) Independent of scheduling and interleaving Without additional synchronization
    29. 29. synchronized • The primary mechanism for synchronization volatile • Enables the thread to bypass the cache when accessing the data
    30. 30. Multithreaded GUIs
    31. 31. • GUI systems use a single thread called the Event Dispatch Thread for handling GUI events • Multithreaded GUIs tend to be particularly prone to deadlocks. • Swing is single-threaded particularly because of observations of AWT which tried to be provide a higher degree of multi threaded access. • Longer running tasks triggered by event listeners in the even dispatch thread are switched over to different threads to ensure the GUI doesn’t freeze.
    32. 32. What we looked at
    33. 33. • Concurrency and Parallelism in the Java Runtime Environment • The Java Thread Model - Decisions for Design • Implementing Multithreading – Creating and Managing Threads • Remote Method Execution – Java RMI • The Concurrency Utilities – java.util.concurrent • Thread Safety – Shared Access to Data • GUI Applications in Java
    34. 34. • Android – A Java based mobile application platform • Java Swing, AWT and Applets • Process scheduling in Java Operating Systems
    35. 35. Time for Questions and Comments
    36. 36. • Tim Peierls, Brian Goetz, Joshua Bloch, Joseph Bowbeer, Doug Lea, and David Holmes. 2005. Java Concurrency in Practice. Addison-Wesley Professional. • Kazuaki Ishizaki, Shahrokh Daijavad, and Toshio Nakatani. 2011. Refactoring Java programs using concurrent libraries. In Proceedings of the Workshop on Parallel and Distributed Systems: Testing, Analysis, and Debugging (PADTAD '11) • Dan Grossman and Ruth E. Anderson. 2012. Introducing parallelism and concurrency in the data structures course. In Proceedings of the 43rd ACM technical symposium on Computer Science Education (SIGCSE '12)
    37. 37. • Alan D. Fekete. 2008. Teaching students to develop thread-safe java classes. In Proceedings of the 13th annual conference on Innovation and technology in computer science education (ITiCSE '08) • Doug Lea. Concurrent Programming in Java: Design principles and patterns, Addison-Wesley. First edition, October 1996 (also, German, Chinese, and Japanese translations, and an on-line supplement). Second edition, November 1999. • Brian Goetz, with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea, Java Concurrency in Practice, Addison Wesley, 2006.
    1. A particular slide catching your eye?

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

    ×