Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 6: Process Synchronization
1 Java threads: synchronization. 2 Thread states 1.New: created with the new operator (not yet started) 2.Runnable: either running or ready to run 3.Blocked:
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Java Programming: Advanced Topics
1 Java Threads and Synchronization Review Modified from slides taken from
1 Java Threads Instructor: Mainak Chaudhuri
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads Representation and Management of Data on the Internet.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Threads Mehdi Einali Advanced Programming in Java 1.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Java Threads DBI – Representation and Management of Data on the Internet.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Advanced Programming Concurrency and Threads Advanced Programming. All slides copyright: Chetan Arora.
Java.util.concurrent package. concurrency utilities packages provide a powerful, extensible framework of high-performance threading utilities such as.
Multithreading The objectives of this chapter are:
Advanced Programming in Java
Multithreading / Concurrency
Multi Threading.
Background on the need for Synchronization
Multithreaded Programming in Java
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Advanced Programming Behnam Hatami Fall 2017.
Multithreading Chapter 23.
Advanced Programming in Java
Multithreading.
Multithreading.
برنامه‌نویسی چندنخی Multi-Thread Programming
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
Representation and Management of Data on the Internet
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Sadegh Aliakbary

Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Agenda Need for multi-thread programming Threads in java Samples Synchronization synchronized wait & notify join 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Sequential Programming Up to this point, we learned sequential programming. Everything in a program happens one step at a time. What is wrong with this approach? 4JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Multitasking and Multithreading Multitasking refers to a computer's ability to perform multiple jobs concurrently more than one program are running concurrently, e.g., UNIX A thread is a single sequence of execution within a program Multithreading refers to multiple threads of control within a single program each program can run multiple threads of control within it, e.g., Web Browser 5 JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Concurrency vs. Parallelism CPUCPU1CPU2 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Threads and Processes CPU Process 1Process 3Process 2Process 4 main run GC 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

What are Threads Good For? To maintain responsiveness of an application during a long running task. To enable cancellation of separable tasks. Some problems are intrinsically parallel. To monitor status of some resource (DB). 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Parallel Processing Multi-Processor Systems Multi-core CPUs Dual core Core2duo Corei7, corei5 Even with no multi-core processors, Multithreading is useful How? I/O bounded tasks Responsive UI Simulated multi-threading 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

OS Support Multi-task OS Windows & Unix Multi-thread OS Single task OS DOS 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Language Support Some languages have no built-in mechanism for muli- threading C, C++, … QT as a solution OS-dependent libraries pthread in linux Windows API Java has multi-threading in its core language Pros and cons ISA experience 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Application Thread When we execute an application: The JVM creates a Thread object whose task is defined by the main() method It starts the thread The thread executes the statements of the program one by one until the method returns and the thread dies 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Multiple Threads in an Application Each thread has its private run-time stack If two threads execute the same method, each will have its own copy of the local variables the methods uses However, all threads see the same dynamic memory (heap) Two different threads can act on the same object and same static fields concurrently 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Creating Threads There are two ways to create our own Thread object 1. Subclassing the Thread class and instantiating a new object of that class 2. Implementing the Runnable interface In both cases the run() method should be implemented 14 JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Extending Thread public class ThreadExample extends Thread { public void run() { for (int i = 1; i <= 100; i++) { System.out.println("Thread: " + i); } 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Thread Methods void start() Creates a new thread and makes it runnable This method can be called only once void run() The new thread begins its life inside this method 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Thread Methods sleep(int m)/sleep(int m,int n) The thread sleeps for m milliseconds, plus n nanoseconds yield() Causes the currently executing thread object to temporarily pause and allow other threads to execute Allow only threads of the same priority to run Nothing is guaranteed for this method 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Implementing Runnable public class RunnableExample implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System.out.println ("Runnable: " + i); } 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

A Runnable Object The Thread object’s run() method calls the Runnable object’s run() method Allows threads to run inside any object, regardless of inheritance 19JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Starting the Threads public class ThreadsStartExample { public static void main (String argv[]) { new ThreadExample ().start (); new Thread(new RunnableExample ()).start (); } 20JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Scheduling Threads I/O operation completes start() Currently executed thread Ready queue Waiting for I/O operation to be completed Waiting to be notified Sleeping Waiting to enter a synchronized section Newly created threads What happens when a program with a ServerSocket calls accept()? 21

More Thread States getState() method in Thread 22JAVACUP.ir

Alive Thread State Diagram New ThreadDead Thread Running Runnable new ThreadExample(); run() method returns while (…) { … } Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor thread.start(); 23

class ThreadExample extends Thread { public void run() { MultiThreading.task("Thread"); } class RunnableExample implements Runnable{ public void run() { MultiThreading.task("Runnable"); } public class MultiThreading { public static void task(String taskName){ for (int i = 1; i <= 10; i++) { System.out.println(taskName + ": " + i); try { Thread.sleep(new Random().nextInt(10)); } catch (InterruptedException e) { e.printStackTrace(); } 24JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Running the Threads ThreadExample thr1 = new ThreadExample(); thr1.start(); RunnableExample run1 = new RunnableExample(); new Thread(run1).start(); ThreadExample thr2 = new ThreadExample(); thr2.start(); RunnableExample run2 = new RunnableExample(); new Thread(run2).start(); 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Output First Run Second Run … 1. Thread: 7 2. Runnable: 7 3. Thread: 9 4. Runnable: 9 5. Thread: Thread: 8 7. Runnable: 8 8. Runnable: Thread: Runnable: Runnable: Thread: 10 … 1. Thread: 8 2. Runnable: 9 3. Thread: 9 4. Runnable: 7 5. Thread: 8 6. Runnable: 8 7. Thread: 9 8. Thread: Runnable: Thread: Runnable: Runnable: 10 26JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

GUI Example Start Counting  starts counting the counter Stop Counting  stops counting the counter 27JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

UnresponsiveUI StartButton: startButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { stop = false; for (int i = 0; i < ; i++) { if (stop) break; tfCount.setText("" + countValue); countValue++; } }); 28JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

UnresponsiveUI (2) StopButton: stopButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { stop = true; } }); 29JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

ResponsiveUI btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { stop = false; Thread t = new Thread() { public void run() { for (int i = 0; i < ; i++) { if (stop) break; tfCount.setText("" + countValue); countValue++; try { sleep(10); } catch (InterruptedException ex) {} } }; t.start(); } }); 30JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Java Scheduling Thread scheduling is the mechanism used to determine how runnable threads are allocated CPU time Scheduler is based on priority of threads The priority of a thread : the importance of a thread to the scheduler Uses fixed-priority scheduling: Threads are scheduled according to their priority Priority is compared with other threads in the ready queue 31JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Thread Priority The scheduler will lean toward running the waiting thread with the highest priority first Lower-priority threads just tend to run less often The exact behavior depends on the platform Usually, all threads should run at the default priority Trying to manipulate thread priorities is usually a mistake 32JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Thread Priority (2) Every thread has a priority When a thread is created, it inherits the priority of the thread that created it The priority values range from 1 to 10, in increasing priority 33JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Thread Priority (3) The priority can be adjusted subsequently using the setPriority() method The priority of a thread may be obtained using getPriority() Priority constants are defined: MIN_PRIORITY=1 MAX_PRIORITY=10 NORM_PRIORITY=5 34JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Some Notes Thread implementation in Java is actually based on operating system support Some Windows operating systems support only 7 priority levels, so different levels in Java may actually be mapped to the same operating system level 35JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Daemon Threads Daemon threads are “background” threads, that provide services to other threads, e.g., the garbage collection thread The Java VM will not exit if non-Daemon threads are executing The Java VM will exit if only Daemon threads are executing Daemon threads die when the Java VM exits A thread becomes a daemon with setDaemon() method 36JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Concurrency An object in a program can be changed by more than one thread Q: Is the order of changes that were preformed on the object important? 38JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Race Condition A race condition – the outcome of a program is affected by the order in which the program's threads are allocated CPU time Two threads are simultaneously modifying a single object Both threads “race” to store their value 39JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Race Condition Example Put green pieces Put red pieces How can we have alternating colors? 40JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Monitors Each object has a “monitor” that is a token used to determine which application thread has control of a particular object instance In execution of a synchronized method (or block), access to the object monitor must be gained before the execution Access to the object monitor is queued 41JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Monitor (cont.) Entering a monitor is also referred to as locking the monitor, or acquiring ownership of the monitor If a thread A tries to acquire ownership of a monitor and a different thread has already entered the monitor, the current thread (A) must wait until the other thread leaves the monitor 42JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Critical Section The synchronized methods define critical sections Execution of critical sections is mutually exclusive. Why? 43JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example public class BankAccount { private float balance; public synchronized void deposit(float amount) { balance += amount; } public synchronized void withdraw(float amount) { balance -= amount; } 44JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Static Synchronized Methods Marking a static method as synchronized, associates a monitor with the class itself The execution of synchronized static methods of the same class is mutually exclusive. Why? 45JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Synchronized Statements A monitor can be assigned to a block It can be used to monitor access to a data element that is not an object, e.g., array Example: byte[] array; void arrayShift(int count) { synchronized(array) { System.arraycopy (array, count,array, 0, array.size - count); } } 46JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Two Identical Methods private synchronized void g() { h(); } Private void g() { synchronized(this){ h(); } 47JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Join() A method can wait for finishing another thread Using thread.join() 48JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Wait and Notify Allows two threads to cooperate Based on a single shared lock object Marge put a cookie notify Homer and wait Homer eat a cookie notify Marge and wait 49JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

The wait() Method The wait() method is part of the java.lang.Object interface It requires a lock on the object’s monitor to execute It must be called from a synchronized method, or from a synchronized segment of code. In other words: The current thread should have acquired a lock on this object before calling any of the wait methods Otherwise: IllegalMonitorStateException 50JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

The wait() Method wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object Upon call for wait(), the thread releases ownership of this monitor and waits until another thread notifies the waiting threads of the object 51JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

The wait() Method wait() is also similar to yield() Both take the current thread off the execution stack and force it to be rescheduled However, wait() is not automatically put back into the scheduler queue notify() must be called in order to get a thread back into the scheduler’s queue 52JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

notify() and notifyAll() The need to the clock Similar to wait() method: The current thread should have acquired a lock on this object before calling notify() or notifyAll() Otherwise: IllegalMonitorStateException the task calling wait( ), notify( ), or notifyAll( ) must "own" (acquire) the lock for the object before it can call any of those methods. 53JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

wait() and the Lock The object lock is released during the wait( ) 54JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Some Notes Thread-safe Classes E.g, StringBuffer is thread-safe public synchronized StringBuffer append(String str){…} and StringBuilder is NOT thread-safe public StringBuilder append(String str) {…} Threading Problems Starvation Deadlock 55JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example: Producer/Consumer The producer–consumer problem is a classic example of a multi-process synchronization problem The problem: two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. 56JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

High-level Concurrency APIs Low-level threads management: synchronization, wait, notify, … Since 5.0: Java also supports high-level concurrency APIs In its java.util.concurrent package These high-level APIs exploit today’s multi-core hardware 58JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Synchronizer Classes Semaphore CountDownLatch Exchanger CyclicBarrier 59JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Semaphore A semaphore controls access to shared resources. It maintains a counter to specify the number of resources that the semaphore controls. Main methods: acquire() and release() 60JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Semaphore Example int i=1; Semaphore sem = new Semaphore(1); sem.release(); System.out.println(i++); sem.release(); System.out.println(i++); sem.acquire(); System.out.println(i++); sem.acquire(); System.out.println(i++); sem.acquire(); System.out.println(i++); sem.acquire(); System.out.println(i++); sem.acquire(); System.out.println(i++); 61 The output: Prints 1..5 and then waits… JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Applications of Semaphore A useful way to think of a semaphore: How many units of a particular resource are available E.g., in Producer/Consumer problem: The consumer checks a semaphore (initialized by zero) The producer checks another semaphore (initialized by the buffer size) 62JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

CountDownLatch This synchronizer allows one or more threads to wait for a countdown to complete. This countdown could be for a set of events to happen or until a set of operations being performed in other threads completes. Main methods: await() and countDown() 63JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

CountDownLatch (2) CountDownLatch latch = new CountDownLatch(number); latch.countDown(); latch.await(); System.out.println("Finished!"); Number>2  unlimitted wait Number<=2  Finished! Note: in countDown() method, if the current count is already zero, nothing happens. 64JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Exchanger The Exchanger class is meant for exchanging data between two threads. Very simple: it waits until both the threads have called the exchange() method. 65JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example for Exchanger 66 In run() method of the two threads:

CyclicBarrier Allows a set of threads to all wait for each other to reach a common barrier point CyclicBarriers are useful when a fixed number of threads must occasionally wait for each other Constructor: CyclicBarrier(int numThreads) Main method: await() 67JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

CyclicBarrier The barrier is called cyclic, because it can be re-used after the waiting threads are released. 68JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Concurrent Collections A number of classes in java.util.concurrent package: Are thread-safe equivalents of collections framework classes in the java.util package For example: java.util.concurrent.ConcurrentHashMap 69JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

BlockingQueue An interface that extends the Queue interface if the queue is empty, it waits (i.e., blocks) for an element to be inserted and if the queue is full, it waits for an element to be removed ArrayBlockingQueue Fixed-sized array-based implementation of BlockingQueue LinkedBlockingQueue a linked-list-based implementation of BlockingQueue 70JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Other Concurrent Collections DelayQueue PriorityBlockingQueue SynchronousQueue LinkedBlockingDeque ConcurrentHashMap ConcurrentSkipListMap ConcurrentSkipListSet CopyOnWriteArrayList CopyOnWriteArraySet … 71JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Atomic Variables Suppose a multi-threaded program that Acquires and releases locks for implementing primitive/simple operations like incrementing a variable, decrementing a variable, and … Such acquiring and releasing of locks for such primitive operations is not efficient an efficient alternative: atomic variables Provided in java.util.concurrent.atomic package 72JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Atomic Variable Classes AtomicBoolean AtomicInteger (extends from Number) AtomicIntegerArray AtomicLong (extends from Number) AtomicLongArray AtomicReference AtomicReferenceArray 73JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Example: AtomicInteger methods AtomicInteger() AtomicInteger(int initVal) int get() void set(int newVal) int getAndSet(int newValue) boolean compareAndSet (int expect, int update) int getAndIncrement() int getAndDecrement() … 74JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Locks The java.util.concurrent.locks package Provides facilities that are similar to “synchronized”, but are more sophisticated Using synchronized: locking implicitly Using Lock objects: locking explicitly 75JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Locks (2) Using a Lock object is similar to obtaining implicit locks using the synchronized keyword. The aim of both constructs is the same: to ensure that only one thread accesses a shared resource at a time However, unlike the synchronized keyword, Locks also support the wait/notify mechanism Along with its support for Condition objects 76JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Lock vs. synchronized (1) Manual release of lock Lock lock = /* get Lock type instance */; lock.lock(); try { // critical section } finally { lock.unlock(); } 77JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Lock vs. synchronized (2) You can do a “non-blocking attempt” Using the tryLock() 78JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Implementations of Lock interface ReentrantLock E.g.: Lock lockObject = new ReentrantLock(); ReadLock The lock returned by method ReentrantReadWriteLock.readLock() WriteLock The lock returned by method ReentrantReadWriteLock.writeLock() 79JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

ReadWriteLock interface Methods: Lock readLock(); Lock writeLock(); Implementation Class: ReentrantReadWriteLock ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); rwl.readLock().lock(); rwl.writeLock().lock(); 80JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Summary Low-level API Synchronized, wait, notify, join, … High-level API java.util.concurrent package Synchronizer Classes (Semaphore, …) Concurrent Collections (ConcurrentHashMap, …) Atomic Variables java.util.concurrent.atomic package AtomicBoolean, AtomicInteger, … Locks java.util.concurrent.locks package 81JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

82JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Phaser Phaser is a useful feature when: Few independent threads have to work in phases to complete a task Since Java 7 83JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

About Phaser A reusable synchronization barrier Similar in functionality to CyclicBarrier and CountDownLatch But supporting more flexible usage. 84JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source

Conditions A Condition supports thread notification mechanism When a certain condition is not satisfied, a thread can wait for another thread to satisfy that condition; That other thread could notify once the condition is met. A condition is bound to a lock. A Condition object offers three methods to support wait/notify pattern: await(), signal(), and signalAll(). 85JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source