threads mehdi einali advanced programming in java 1

107
Threads Mehdi Einali Advanced Programming in Java 1

Upload: audra-carr

Post on 19-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Threads Mehdi Einali Advanced Programming in Java 1

1

Threads

Mehdi Einali

Advanced Programming in Java

Page 2: Threads Mehdi Einali Advanced Programming in Java 1

2

AgendaConcurrencyThreads in java

Problems in concurrencySolution for problems in concurrencyProblems in Solutions for problems in concurrency

Java High-level API for concurrency

Page 3: Threads Mehdi Einali Advanced Programming in Java 1

3

concurrency

Page 4: Threads Mehdi Einali Advanced Programming in Java 1

4

Sequential ProgrammingUp to this point, we learned sequential programming. Everything in a program happens one step at a time.What is wrong with this approach?

Page 5: Threads Mehdi Einali Advanced Programming in Java 1

5

Multitasking vs single task

A single-tasking system can only run one program at a time, while a multi-tasking operating system allows more than one program to be running in concurrency.

Single task OS:

Multiple task OS:

Page 6: Threads Mehdi Einali Advanced Programming in Java 1

6

OS Support

Page 7: Threads Mehdi Einali Advanced Programming in Java 1

7

Concurrency vs. Parallelism

CPU CPU1 CPU2

Page 8: Threads Mehdi Einali Advanced Programming in Java 1

8

Concurrency vs parallelism

Page 9: Threads Mehdi Einali Advanced Programming in Java 1

9

concurency

Page 10: Threads Mehdi Einali Advanced Programming in Java 1

10

Thread vs processA process is an executing instance of an program.

Processes contain their own state information, use their own address spaces, and only interact with each other via inter-process communication mechanisms (generally managed by the operating system)

A thread is a single sequence of execution within a program

Threads within a process share the same state and same memory space, and can communicate with each other directly, because they share the same variables

Page 11: Threads Mehdi Einali Advanced Programming in Java 1

11

Thread vs process

Page 12: Threads Mehdi Einali Advanced Programming in Java 1

12

Jvm is multi threaded

CPU

Process 1 Process 3Process 2 Process 4

main

run

GC

Page 13: Threads Mehdi Einali Advanced Programming in Java 1

13

Multithreading

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

Page 14: Threads Mehdi Einali Advanced Programming in Java 1

14

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).

Page 15: Threads Mehdi Einali Advanced Programming in Java 1

15

Parallel ProcessingMulti-Processor SystemsMulti-core CPUs

Dual coreCore2duoCorei7, corei5

Even with no multi-core processors, Multithreading is useful

How?I/O bounded tasksResponsive UISimulated multi-threading

Page 16: Threads Mehdi Einali Advanced Programming in Java 1

16

Language SupportSome languages have no built-in mechanism for muli-threading

C, C++, …QT as a solution

OS-dependent librariespthread in linuxWindows API

Java has multi-threading in its core language

Pros and cons

Page 17: Threads Mehdi Einali Advanced Programming in Java 1

17

Threads in java

Page 18: Threads Mehdi Einali Advanced Programming in Java 1

18

Application ThreadWhen we execute an application:

The JVM creates a Thread object whose task is defined by the main() method It starts the threadThe thread executes the statements of the program one by one until the method returns and the thread dies

Page 19: Threads Mehdi Einali Advanced Programming in Java 1

19

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 usesHowever, all threads see the same dynamic memory (heap)Two different threads can act on the same object and same static fields concurrently

Page 20: Threads Mehdi Einali Advanced Programming in Java 1

20

Creating ThreadsThere 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

Page 21: Threads Mehdi Einali Advanced Programming in Java 1

21

Extending Thread

public class ThreadExample extends Thread {public void run() {

for (int i = 1; i <= 100; i++) { System.out.println("Thread: " + i);}

}}

Page 22: Threads Mehdi Einali Advanced Programming in Java 1

22

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

Page 23: Threads Mehdi Einali Advanced Programming in Java 1

23

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

Page 24: Threads Mehdi Einali Advanced Programming in Java 1

24

Implementing Runnablepublic class RunnableExample implements Runnable {

public void run () { for (int i = 1; i <= 100; i++) {

System.out.println ("Runnable: " + i);

} }

}

Page 25: Threads Mehdi Einali Advanced Programming in Java 1

25

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

Page 26: Threads Mehdi Einali Advanced Programming in Java 1

26

Starting the Threads

public class ThreadsStartExample { public static void main (String argv[]) {

new ThreadExample ().start (); new Thread(new RunnableExample ()).start ();

}}

Page 27: Threads Mehdi Einali Advanced Programming in Java 1

27

Scheduling Threads

I/O operation completes

start()

Currently executedthread

Ready queue

• Waiting for I/O operation to be completed• Waiting to be notified• Sleeping• Waiting to enter a synchronized section

Newly createdthreads

What happens when a program with aServerSocket callsaccept()?

Page 28: Threads Mehdi Einali Advanced Programming in Java 1

28

More Thread States

Page 29: Threads Mehdi Einali Advanced Programming in Java 1

29

Thread State Diagram

Alive

New Thread Dead Thread

Running

Runnable

new ThreadExample();

run() method returns

while (…) { … }

BlockedObject.wait()Thread.sleep()blocking IO callwaiting on a monitor

thread.start();

Page 30: Threads Mehdi Einali Advanced Programming in Java 1

30

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(); }}}

Page 31: Threads Mehdi Einali Advanced Programming in Java 1

31

Running the ThreadsThreadExample 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();

Page 32: Threads Mehdi Einali Advanced Programming in Java 1

32

Output

First Run • Second Run…

1. Thread: 7

2. Runnable: 7

3. Thread: 9

4. Runnable: 9

5. Thread: 10

6. Thread: 8

7. Runnable: 8

8. Runnable: 10

9. Thread: 9

10. Runnable: 9

11. Runnable: 10

12. 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: 10

9. Runnable: 10

10. Thread: 10

11. Runnable: 9

12. Runnable: 10

Page 33: Threads Mehdi Einali Advanced Programming in Java 1

33

responsiveness

Page 34: Threads Mehdi Einali Advanced Programming in Java 1

34

GUI ExampleStart Counting starts counting the counterStop Counting stops counting the counter

Page 35: Threads Mehdi Einali Advanced Programming in Java 1

35

Unresponsive UIStartButton:

startButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) { this.stop = false; for (int i = 0; i < 100000; i++) { if (this.stop) break; tfCount.setText("" + countValue); countValue++; } } });

Page 36: Threads Mehdi Einali Advanced Programming in Java 1

36

Unresponsive UI (2)

StopButton:

stopButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

this.stop = true; }

});

Page 37: Threads Mehdi Einali Advanced Programming in Java 1

37

Responsive UIbtnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { this.stop = false; Thread counter = new CounterThread();

counter.start(); }});

Inner classclass CounterThread extends Thread { public void run() { for (int i = 0; i < 100000; i++) { if (this.stop){ break;} tfCount.setText("" + countValue); countValue++;

try { sleep(10); } catch (InterruptedException ex) {} } } }

Page 38: Threads Mehdi Einali Advanced Programming in Java 1

38

Responsive UIbtnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {

this.stop = false;new Thread() {

public void run() { for (int i = 0; i < 100000; i++) { if (this.stop){ break;} tfCount.setText("" + countValue); countValue++;

try { sleep(10); } catch (InterruptedException ex) {} } } }.start();

}}); • Define anonymous inner class

• Create object • Start thread

Page 39: Threads Mehdi Einali Advanced Programming in Java 1

39

Thread scheduling

Page 40: Threads Mehdi Einali Advanced Programming in Java 1

40

Java SchedulingThread scheduling is the mechanism used to determine how runnable threads are allocated CPU timeScheduler is based on priority of threadsThe priority of a thread : the importance of a thread to the schedulerUses fixed-priority scheduling:

Threads are scheduled according to their priority Priority is compared with other threads in the ready queue

Page 41: Threads Mehdi Einali Advanced Programming in Java 1

41

Thread PriorityThe scheduler will lean toward running the waiting thread with the highest priority firstLower-priority threads just tend to run less oftenThe exact behavior depends on the platformUsually, all threads should run at the default priorityTrying to manipulate thread priorities is usually a mistake

Page 42: Threads Mehdi Einali Advanced Programming in Java 1

42

Thread Priority (2)Every thread has a priorityWhen a thread is created, it inherits the priority of the thread that created itThe priority values range from 1 to 10, in increasing priority

Page 43: Threads Mehdi Einali Advanced Programming in Java 1

43

Thread Priority (3)The priority can be adjusted subsequently using the setPriority() methodThe priority of a thread may be obtained using getPriority()

Priority constants are defined: MIN_PRIORITY=1MAX_PRIORITY=10NORM_PRIORITY=5

Page 44: Threads Mehdi Einali Advanced Programming in Java 1

44

Some NotesThread implementation in Java is actually based on operating system supportSome Windows operating systems support only 7 priority levels, so different levels in Java may actually be mapped to the same operating system level

Page 45: Threads Mehdi Einali Advanced Programming in Java 1

45

Daemon ThreadsDaemon threads are “background” threads, that provide services to other threads, e.g., the garbage collection threadThe Java VM will not exit if non-Daemon threads are executingThe Java VM will exit if only Daemon threads are executingDaemon threads die when the Java VM exitsA thread becomes a daemon with setDaemon() method

Page 46: Threads Mehdi Einali Advanced Programming in Java 1

46

quiz

Which one is impossible output?

A) 1234567B) 3416527C) 1334567D) 7654321E) 0124567F) none of them

Page 47: Threads Mehdi Einali Advanced Programming in Java 1

47

Problems in concurrency

Page 48: Threads Mehdi Einali Advanced Programming in Java 1

48

Shared dataAn object in a program can be changed by more than one threadQ: Is the order of changes that were preformed on the object important?

Page 49: Threads Mehdi Einali Advanced Programming in Java 1

49

Race ConditionA race condition – the outcome of a program is affected by the order in which the program's threads are allocated CPU timeTwo threads are simultaneously modifying a single object

Both threads “race” to store their value

Page 50: Threads Mehdi Einali Advanced Programming in Java 1

50

Race Condition Example

A

B

C

Read A

Decrement A

Check Condition(B)

Increment(B)

Check Condition(C)

Increment(C)

Read A

Decrement A

Check Condition(B)

Increment(A)

Page 51: Threads Mehdi Einali Advanced Programming in Java 1

51

Dirty read

Read A

Decrement A

Read A

Decrement A

Time Thread IIThread I

Page 52: Threads Mehdi Einali Advanced Programming in Java 1

52

Lost updateTime Thread IIThread I

Read A

Decrement A

Read A

Decrement A

Check Condition(B)

Increment(A)

Page 53: Threads Mehdi Einali Advanced Programming in Java 1

53

Solution for problems in concurrency

Page 54: Threads Mehdi Einali Advanced Programming in Java 1

54

MonitorsEach object has a “monitor” that is a token used to determine which application thread has control of a particular object instanceIn execution of a synchronized method (or block), access to the object monitor must be gained before the executionAccess to the object monitor is queued

Page 55: Threads Mehdi Einali Advanced Programming in Java 1

55

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

Page 56: Threads Mehdi Einali Advanced Programming in Java 1

56

Critical SectionThe synchronized methods define critical sectionsExecution of critical sections is mutually exclusive. Why?

Page 57: Threads Mehdi Einali Advanced Programming in Java 1

57

Examplepublic class BankAccount {

private float balance;

public synchronized void deposit(float amount) { balance += amount; } public synchronized void withdraw(float amount)

{ balance -= amount; } }

Page 58: Threads Mehdi Einali Advanced Programming in Java 1

58

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?

Page 59: Threads Mehdi Einali Advanced Programming in Java 1

59

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

byte[] array;

void arrayShift(int count) {

synchronized(array) {

System.arraycopy (array,

count,array, 0, array.size - count);

}

}

Page 60: Threads Mehdi Einali Advanced Programming in Java 1

60

Two Identical Methods

private synchronized void g() {h();

}

private void g() {synchronized(this){

h();}

}

Page 61: Threads Mehdi Einali Advanced Programming in Java 1

61

Join()A method can wait for finishing another threadUsing thread.join()

Page 62: Threads Mehdi Einali Advanced Programming in Java 1

62

Wait and NotifyAllows two threads to cooperateBased on a single shared lock object

Marge put a cookie notify Homer and waitHomer eat a cookie notify Marge and wait

Page 63: Threads Mehdi Einali Advanced Programming in Java 1

63

The wait() MethodThe wait() method is part of the java.lang.Object interfaceIt requires a lock on the object’s monitor to executeIt 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 methodsOtherwise: IllegalMonitorStateException

Page 64: Threads Mehdi Einali Advanced Programming in Java 1

64

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

Page 65: Threads Mehdi Einali Advanced Programming in Java 1

65

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

Page 66: Threads Mehdi Einali Advanced Programming in Java 1

66

notify() and notifyAll()The need to the clockSimilar 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.

Page 67: Threads Mehdi Einali Advanced Programming in Java 1

67

wait() and the LockThe object lock is released during the wait( )

Page 68: Threads Mehdi Einali Advanced Programming in Java 1

68

Problems in Solutions for problems in concurrency

Page 69: Threads Mehdi Einali Advanced Programming in Java 1

69

deadlock

Page 70: Threads Mehdi Einali Advanced Programming in Java 1

70

starvationSome locks exchanged by specific threads and other ones starve

Page 71: Threads Mehdi Einali Advanced Programming in Java 1

71

Example: Producer/Consumer

The producer–consumer problem is a classic example of a multi-process synchronization problemThe problem:

two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.

Page 72: Threads Mehdi Einali Advanced Programming in Java 1

72

Thread safeThread-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) {…}

Page 73: Threads Mehdi Einali Advanced Programming in Java 1

73

Concurrent collectionsArrayBlockingQueue<E> A bounded blocking queue backed by an array.

ConcurrentHashMap<K,V>A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.

ConcurrentLinkedQueue<E> An unbounded thread-safe queue based on linked nodes.

CopyOnWriteArrayList<E>A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

CopyOnWriteArraySet<E> A Set that uses CopyOnWriteArrayList for all of its operations.

DelayQueue<E extends Delayed>

An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired.

LinkedBlockingQueue<E> An optionally-bounded blocking queue based on linked nodes.

PriorityBlockingQueue<E>An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations.

SynchronousQueue<E>A blocking queue in which each put must wait for a take, and vice versa.

Page 74: Threads Mehdi Einali Advanced Programming in Java 1

74

New Concurrency Classes in Java

Page 75: Threads Mehdi Einali Advanced Programming in Java 1

75

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 packageThese high-level APIs exploit today’s multi-core hardware

Page 76: Threads Mehdi Einali Advanced Programming in Java 1

76

Synchronizer ClassesSemaphoreCountDownLatchExchangerCyclicBarrier

Page 77: Threads Mehdi Einali Advanced Programming in Java 1

77

SemaphoreA 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()

Page 78: Threads Mehdi Einali Advanced Programming in Java 1

78

exampleSemaphore semaphore = new Semaphore(1);

semaphore.acquire(); //critical section

...

semaphore.release();

10

1

Page 79: Threads Mehdi Einali Advanced Programming in Java 1

79

Semaphore Exampleint 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++);

The output:

Prints 1..5 and then waits…

Page 80: Threads Mehdi Einali Advanced Programming in Java 1

80

Applications of SemaphoreA useful way to think of a semaphore:How many units of a particular resource are availableE.g., in Producer/Consumer problem:

The consumer checks a semaphore (initialized by zero)The producer checks another semaphore

(initialized by the buffer size)

Page 81: Threads Mehdi Einali Advanced Programming in Java 1

81

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()

Page 82: Threads Mehdi Einali Advanced Programming in Java 1

82

use

Page 83: Threads Mehdi Einali Advanced Programming in Java 1

83

Page 84: Threads Mehdi Einali Advanced Programming in Java 1

84

Note: in countDown() method, if the current count is already zero, nothing happens.

Page 85: Threads Mehdi Einali Advanced Programming in Java 1

85

ExchangerThe Exchanger class is meant for exchanging data between two threads.a kind of rendezvous point where two threads can exchange objectsVery simple: it waits until both the threads have called the exchange() method.

Page 86: Threads Mehdi Einali Advanced Programming in Java 1

86

Example for Exchanger

Page 87: Threads Mehdi Einali Advanced Programming in Java 1

87

Page 88: Threads Mehdi Einali Advanced Programming in Java 1

88

output

Page 89: Threads Mehdi Einali Advanced Programming in Java 1

89

CyclicBarrierAllows a set of threads to all wait for each other to reach a common barrier pointCyclicBarriers are useful when a fixed number of threads must occasionally wait for each other

Constructor: CyclicBarrier(int numThreads)Main method: await()

Page 90: Threads Mehdi Einali Advanced Programming in Java 1

90

CyclicBarrier

The barrier is called cyclic, because it can be re-used after the waiting threads are released.

Page 91: Threads Mehdi Einali Advanced Programming in Java 1

91

More You can also specify a timeout for the waiting thread.

barrier.await(10, TimeUnit.SECONDS);

The CyclicBarrier supports a barrier action, which is a Runnable that is executed once the last thread arrives

Runnable barrierAction = ... ; CyclicBarrier barrier = new CyclicBarrier(2, barrierAction);

Page 92: Threads Mehdi Einali Advanced Programming in Java 1

92

usage

Page 93: Threads Mehdi Einali Advanced Programming in Java 1

93

Page 94: Threads Mehdi Einali Advanced Programming in Java 1

94

Note that the sequence in which the threads gets to write to the console may vary from execution to execution.

Sometimes Thread-0 prints first, sometimesThread-1 prints first etc.

output

Page 95: Threads Mehdi Einali Advanced Programming in Java 1

95

Concurrent CollectionsA 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

Page 96: Threads Mehdi Einali Advanced Programming in Java 1

96

BlockingQueueBlockingQueue

An interface that extends the Queue interface if the queue is empty, it waits (i.e., blocks) for an element to be insertedand if the queue is full, it waits for an element to be removed

ArrayBlockingQueueFixed-sized array-based implementation of BlockingQueue

LinkedBlockingQueuea linked-list-based implementation of BlockingQueue

Page 97: Threads Mehdi Einali Advanced Programming in Java 1

97

Other Concurrent Collections

DelayQueuePriorityBlockingQueueSynchronousQueueLinkedBlockingDequeConcurrentHashMapConcurrentSkipListMapConcurrentSkipListSetCopyOnWriteArrayListCopyOnWriteArraySet…

Page 98: Threads Mehdi Einali Advanced Programming in Java 1

98

Atomic VariablesSuppose 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 efficientan efficient alternative: atomic variablesProvided in java.util.concurrent.atomic package

Page 99: Threads Mehdi Einali Advanced Programming in Java 1

99

Atomic Variable ClassesAtomicBooleanAtomicInteger (extends from Number)AtomicIntegerArrayAtomicLong (extends from Number)AtomicLongArrayAtomicReferenceAtomicReferenceArray

Page 100: Threads Mehdi Einali Advanced Programming in Java 1

100

Example: Atomic Integer 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()…

Page 101: Threads Mehdi Einali Advanced Programming in Java 1

101

LocksThe 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

Page 102: Threads Mehdi Einali Advanced Programming in Java 1

102

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

Page 103: Threads Mehdi Einali Advanced Programming in Java 1

103

Lock vs. synchronized (2)You can do a “non-blocking attempt”Using the tryLock()

Page 104: Threads Mehdi Einali Advanced Programming in Java 1

104

Implementations of Lock interface

ReentrantLockE.g.:

Lock lockObject = new ReentrantLock();

ReadLockThe lock returned by method ReentrantReadWriteLock.readLock()

WriteLockThe lock returned by method ReentrantReadWriteLock.writeLock()

Page 105: Threads Mehdi Einali Advanced Programming in Java 1

105

ReadWriteLock interfaceMethods:

Lock readLock();Lock writeLock();

Implementation Class: ReentrantReadWriteLock

ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();rwl.readLock().lock();rwl.writeLock().lock();

Page 107: Threads Mehdi Einali Advanced Programming in Java 1

107

end