Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming in Java

Similar presentations


Presentation on theme: "Advanced Programming in Java"— Presentation transcript:

1 Advanced Programming in Java
Threads Mehdi Einali

2 Agenda Concurrency Threads in java Problems in concurrency
Solution for problems in concurrency Problems in Solutions for problems in concurrency Java High-level API for concurrency

3 concurrency

4 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?

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:

6 OS Support

7 Concurrency vs. Parallelism
CPU CPU1 CPU2

8 Concurrency vs parallelism

9 concurency

10 Thread vs process A 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

11 Thread vs process

12 Jvm is multi threaded CPU Process 1 Process 2 Process 3 Process 4 main
run GC

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

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

15 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

16 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

17 Threads in java

18 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

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

20 Creating Threads There are two ways to create our own Thread object
Subclassing the Thread class and instantiating a new object of that class Implementing the Runnable interface In both cases the run() method should be implemented

21 Extending Thread public class ThreadExample extends Thread {
public void run() { for (int i = 1; i <= 100; i++) { System.out.println("Thread: " + i); }

22 Thread Methods void start() void run()
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

23 Thread Methods sleep(int m)/sleep(int m,int n) yield()
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

24 Implementing Runnable
public class RunnableExample implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System.out.println ("Runnable: " + i); }

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

26 Starting the Threads public class ThreadsStartExample {
public static void main (String argv[]) { new ThreadExample ().start (); new Thread(new RunnableExample ()).start (); }

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

28 More Thread States

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

30 class ThreadExample extends Thread { public void run() {
MultiThreading.task("Thread"); } class RunnableExample implements Runnable{ 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(); }}}

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

32 Output Second Run First Run … Thread: 7 Runnable: 7 Thread: 9

33 responsiveness

34 GUI Example Start Counting  starts counting the counter
Stop Counting  stops counting the counter

35 Unresponsive UI StartButton:
startButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { this.stop = false; for (int i = 0; i < ; i++) { if (this.stop) break; tfCount.setText("" + countValue); countValue++; } });

36 Unresponsive UI (2) StopButton:
stopButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { this.stop = true; } });

37 Responsive UI btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) { this.stop = false; Thread counter = new CounterThread(); counter.start(); } }); Inner class class CounterThread extends Thread { public void run() { for (int i = 0; i < ; i++) { if (this.stop){ break;} tfCount.setText("" + countValue); countValue++; try { sleep(10); } catch (InterruptedException ex) {} } } }

38 Responsive UI Define anonymous inner class Create object Start thread
btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { this.stop = false; new Thread() { public void run() { for (int i = 0; i < ; i++) { if (this.stop){ break;} tfCount.setText("" + countValue); countValue++; try { sleep(10); } catch (InterruptedException ex) {} } }.start(); }); Define anonymous inner class Create object Start thread

39 Thread scheduling

40 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

41 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

42 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

43 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

44 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

45 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

46 quiz Which one is impossible output? A) 1234567 B) 3416527 C) 1334567
D) E) F) none of them E

47 Problems in concurrency

48 Shared data 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?

49 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

50 Race Condition Example
Read A Decrement A Check Condition(B) Increment(B) Check Condition(C) Increment(C) Read A Decrement A Check Condition(B) Increment(A) B C A

51 Dirty read Thread I Time Thread II Read A Decrement A Read A

52 Lost update Thread I Time Thread II Read A Decrement A
Check Condition(B) Increment(A) Read A Decrement A

53 Solution for problems in concurrency

54 Critical Section A critical section is a part of a multi-process program that may not be concurrently executed by more than one of the program's processes. Execution of critical sections is mutually exclusive. Why?

55 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 The synchronized methods define critical sections

56 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

57 Example public class BankAccount { private float balance;
public synchronized void deposit(float amount) { balance += amount; } public synchronized void withdraw(float amount) { balance -= amount;

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?

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

60 Two Identical Methods private synchronized void g() { h(); }
private void g() { synchronized(this){

61 Lock control Critical section(synchronized keyword) is critical to handle concurrency but is not enough Synchronized sections: Acquire lock Wait for lock Release lock(by finishing) We need more control on locks Not just with synchronized

62 Join() and yeild A method can wait for finishing another thread
Using otherThread.join() Current thread will wait for otherThread finish its job Thread.yield bring current thread back to runnable queue and other thread with same priority will have chance to run

63 Wait and Notify Allows two threads to cooperate
Based on a single shared lock object

64 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

65 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

66 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

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

68 wait() and the Lock The object lock is released during the wait( )

69

70 Problems in Solutions for problems in concurrency

71 deadlock

72 starvation Some locks exchanged by specific threads and other ones starve

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

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

75 Concurrent collections
ArrayBlockingQueue<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.

76 New Concurrency Classes in Java

77 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

78 Synchronizer Classes Semaphore CountDownLatch Exchanger CyclicBarrier

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

80 example Semaphore semaphore = new Semaphore(1); semaphore.acquire(); //critical section ... semaphore.release(); 1 1

81 Semaphore Example The output: Prints 1..5 and then waits… int i=1;
Semaphore sem = new Semaphore(1); sem.release(); System.out.println(i++); sem.acquire(); The output: Prints 1..5 and then waits…

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

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

84 use

85

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

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

88 Example for Exchanger

89

90 output

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

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

93 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);

94 usage

95

96 output 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, sometimes Thread-1 prints first etc.

97 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

98 BlockingQueue BlockingQueue ArrayBlockingQueue LinkedBlockingQueue
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

99 Other Concurrent Collections
DelayQueue PriorityBlockingQueue SynchronousQueue LinkedBlockingDeque ConcurrentHashMap ConcurrentSkipListMap ConcurrentSkipListSet CopyOnWriteArrayList CopyOnWriteArraySet

100 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

101 Atomic Variable Classes
AtomicBoolean AtomicInteger (extends from Number) AtomicIntegerArray AtomicLong (extends from Number) AtomicLongArray AtomicReference AtomicReferenceArray

102 Example: Atomic Integer methods
AtomicInteger(int initVal) int get() void set(int newVal) int getAndSet(int newValue) boolean compareAndSet (int expect, int update) int getAndIncrement() int getAndDecrement()

103 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

104 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

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

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

107 ReadWriteLock interface
Methods: Lock readLock(); Lock writeLock(); Implementation Class: ReentrantReadWriteLock ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); rwl.readLock().lock(); rwl.writeLock().lock();

108 More on java high level concurrency api

109 end


Download ppt "Advanced Programming in Java"

Similar presentations


Ads by Google