Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Java Network Programming And Distributed Computing

2 Spring 2006Special Topics in Computer Engineering : Threads 2 Topics Threads Synchronization Groups/Priorities Inter-thread communication

3 Spring 2006Special Topics in Computer Engineering : Threads 3 Concurrent Programming What is a Thread? What can go wrong with a Thread? How can we fix it? How does Java handle them? Why would I use them?

4 Spring 2006Special Topics in Computer Engineering : Threads 4 What is a Thread? A Thread is a single, sequential flow of control within a program. Now they are so ubiquitous (yū-bĭk'wĭ-təs) that you don’t notice any them more.  DOS vs. Windows DOS vs. Windows

5 Spring 2006Special Topics in Computer Engineering : Threads 5 ubiquitousubiquitous (yū-bĭk'wĭ-təs) everywhere ever-present omnipresent

6 Spring 2006Special Topics in Computer Engineering : Threads 6 Processes vs. Threads Processes  Completely separate, unrelated concurrent execution on the level of the operating system. (eg multiple programs running at the same time) Threads  Concurrent units of execution within a given program. (eg pulling down a menu while loading a web page within a web browser)

7 Spring 2006Special Topics in Computer Engineering : Threads 7 Thread States New state  Thread was just created Ready state  Thread can now execute In Java, Thread’s start method invoked Running state  Thread is assigned a processor and running In Java, Thread’s run method is executing Done state  Thread has completed or exited  Eventually disposed of by system

8 Spring 2006Special Topics in Computer Engineering : Threads 8 Life cycle of a Thread NewReady Blocked Running Done Thread object is created start method was invoked run method is executing

9 Spring 2006Special Topics in Computer Engineering : Threads 9 Life cycle of a Thread (cont’d) The OS can interrupt the thread at any time while it is running, and allow any other thread to run. Threads can put themselves into a wait state until another thread wakes them up.

10 Spring 2006Special Topics in Computer Engineering : Threads 10 How does Java handle Threads? Subclass java.lang.Thread, or implement java.lang.Runnable. New After you instantiate a thread, it is in the New state. To start running a Thread, call the start method on it. Ready Running  thread transits to Ready state and when OS schedule it for execution it transits to the Running state

11 Spring 2006Special Topics in Computer Engineering : Threads 11 How does Java handle them? (cont’d) To implement Runnable, you need to have a method that override public void run(); This is the method that implements the running section of the thread life-cycle. The thread dies (stops) when the run method terminates.

12 Spring 2006Special Topics in Computer Engineering : Threads 12 How does Java handle them? (cont’d) Blocked The run method may be interrupted at any time by the operating system and put into the Blocked state, but that’s not something you really need to handle.

13 Spring 2006Special Topics in Computer Engineering : Threads 13 How does Java handle them? (cont’d) Run methods generally look like: public class SomeThread extends Thread { public void run() { while (notDone) {…} } public void finish() { notDone = false; } }

14 Spring 2006Special Topics in Computer Engineering : Threads 14 java.lang.Thread Class Provides methods to start suspend resume stop control other aspects (e.g.)  priority  name

15 Spring 2006Special Topics in Computer Engineering : Threads 15 Using the Thread Class Extend Thread class Create a Thread object Start thread running  Invoke Thread.start() method  run() method will be invoked when thread is first started Override the run() method  thread performs useful tasks in the background

16 Spring 2006Special Topics in Computer Engineering : Threads 16 Example ExtendThreadDemo

17 Spring 2006Special Topics in Computer Engineering : Threads 17 What can go wrong? Assuming that threads, existing in the same program, have access to the same variable (e.g. countValue ). What if one is reading data from countValue, gets interrupted, and another one writes to countValue, even though the thread wanted the old value?

18 Spring 2006Special Topics in Computer Engineering : Threads 18 What can you do about it? synchronized is a keyword that can be applied to a method to say that one and only one thread will have access to this method at a time. public synchronized void blah(){ }

19 Spring 2006Special Topics in Computer Engineering : Threads 19 Method–Level Synchronization t1 invokes synchronized method  t1 takes out an object lock (monitor)  t2 attempts to execute synchronized method t2 is BLOCKED  t3,… tn attempt to execute synchronized method, all are BLOCKED and queued t1 returns from synchronized method  lock on the object monitor released  one of the queued threads, ti, access object lock ti starts executing synchronized method

20 Spring 2006Special Topics in Computer Engineering : Threads 20 Example: Counter Update Problem Value of counter  incremented  read At the same time several threads invoke a method to update counter  overwrite each others update A read gets an inaccurate count value

21 Spring 2006Special Topics in Computer Engineering : Threads 21 Counter Update: Solution synchronizing each method that performs a read or write Thread that first invokes the synchronized method locks the counter object’s monitor Lock is released only when thread returns from synchronized method Result: only one thread can update/read counter at a time

22 Spring 2006Special Topics in Computer Engineering : Threads 22 Counter Update: Sample Synchronized Code Sample Synchronized Code

23 Spring 2006Special Topics in Computer Engineering : Threads 23 Block-Level Synchronization Used for preventing concurrent access to resource that is a preexisting class  developer cannot modify by using method- level synchronization A lock is placed around a block of code  block of code is synchronized against a particular object  any thread attempting to enter that block of code is locked out until the monitor for the specified object is released

24 Spring 2006Special Topics in Computer Engineering : Threads 24 Block-Level Synchronization: Syntax { …synchronized (o) { … } }

25 Spring 2006Special Topics in Computer Engineering : Threads 25 More on synchronized public synchronized void blah() {…} Is the same as public void blah (){ synchronized (this){…} }

26 Spring 2006Special Topics in Computer Engineering : Threads 26 java.lang.Runnable interface A better way create a multi-threaded application  Java supports only single inheritance a class that extends the java.lang.Thread class, cannot extend any other class Runnable defines a single method, run(), that must be implemented

27 Spring 2006Special Topics in Computer Engineering : Threads 27 Using Runnable Pass an object implementing Runnable to the constructor of a thread Invoke thread’s start() method  run() method of Runnable will be called by newly created thread  Thread stops executing when run() method terminates

28 Spring 2006Special Topics in Computer Engineering : Threads 28 Using Runnable for Multi-Threading Example: RunnableThreadDemoRunnableThreadDemo

29 Spring 2006Special Topics in Computer Engineering : Threads 29 Illustrating Block-Level Synchronization Block-level synchronization locks against a particular object  multiple blocks can protect access to the same object Block-level synchronization can be applied in thread code wherever an object is accessed or modified

30 Spring 2006Special Topics in Computer Engineering : Threads 30 Block-Level Synchronization Example SynchBlock implements Runnable

31 Spring 2006Special Topics in Computer Engineering : Threads 31 More that can go wrong What happens if you have two things that do this - deadlock public void doSomething() { Synchronized (a) { Synchronized (b) { // code } } } public void doOther() { Synchronized (b) { Synchronized (a) { // code } } }

32 Spring 2006Special Topics in Computer Engineering : Threads 32 Thread examples Test: Ten threads printing their name three times. Test Test2: Main thread joins printing threads. Test2 Test3: Each thread yields after printing. Test3 Test4: Printing threads yield randomly. Test4

33 Spring 2006Special Topics in Computer Engineering : Threads 33 Advanced Thread Features All Java Threads have a priority. If you want a thread to run more relative to other threads, give it a higher priority. Threads can be grouped in ThreadGroup objectsThreadGroup Test7: Two groups of threads Test7 Test8: Printing threads with priority Test8

34 Spring 2006Special Topics in Computer Engineering : Threads 34 Why would I use them? Most advance programs rely on Threads for various tasks. ThreadLister Example 2 cases:  When you want to be doing 2 different things simultaneously.  When you have a large problem that can be broken up and solved in smaller sections, or large I/O bound processes.

35 Spring 2006Special Topics in Computer Engineering : Threads 35 Inter-Thread Communication Sometimes one thread may be interested in the activities of another. Or, one could have a functional dependency on another.  Reading from a file or over a network?  Waiting for a given thread to return a result. Polling (Busy Waiting) vs. Notification  BadConsumer Example BadConsumer

36 Spring 2006Special Topics in Computer Engineering : Threads 36 Waiting for notification As defined in object, every object has a wait(), notify(), and notifyAll() method.  These should never be overridden They can only be called from inside synchronized blocks, and they only effect other threads in synchronized blocks which are synchronized on the same object.

37 Spring 2006Special Topics in Computer Engineering : Threads 37 wait() (cont’d) When a thread enters a wait state, it does nothing until it is notified by another thread. It also gives up it’s lock on the object when wait is called. public synchronized blah() { wait(); … // do something }

38 Spring 2006Special Topics in Computer Engineering : Threads 38 notify() To awaken a thread, a different thread which has a lock on the same object must call notify. When notify is called, the block that had the lock on the object releases it.  Then a thread is awakened from its wait() and can grab the lock and continue processing.

39 Spring 2006Special Topics in Computer Engineering : Threads 39 notify() (cont’d) Note that you don’t specify what is being awoken in notify(). If there are more than 1 thread waiting on the same condition, you have no control of which awakens. notify() only awakens 1 thread. notifyAll() awakens all threads.

40 Spring 2006Special Topics in Computer Engineering : Threads 40 notifyAll() (cont’d) There are two versions - notify() and notifyAll(). Notify is safe only under 2 conditions:  When only 1 thread is waiting, and thus guaranteed to be awakened.  When multiple threads are waiting on the same condition, and it doesn’t matter which one awakens. In general, use notifyAll()


Download ppt "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."

Similar presentations


Ads by Google