Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Handout 10 MULTI-THREADING BY GEORGE KOUTSOGIANNAKIS THIS DOCUMENT CAN NOT BE REPRODUCED OR DISTRIBUTED WITHOUT TH E WRITTEN PERMISSION OF THE AUTHOR.

Similar presentations


Presentation on theme: "1 Handout 10 MULTI-THREADING BY GEORGE KOUTSOGIANNAKIS THIS DOCUMENT CAN NOT BE REPRODUCED OR DISTRIBUTED WITHOUT TH E WRITTEN PERMISSION OF THE AUTHOR."— Presentation transcript:

1 1 Handout 10 MULTI-THREADING BY GEORGE KOUTSOGIANNAKIS THIS DOCUMENT CAN NOT BE REPRODUCED OR DISTRIBUTED WITHOUT TH E WRITTEN PERMISSION OF THE AUTHOR

2 2 Multi-Threading Concurrency: The ability to perform more than one task in parallel within a specified time t. This means that two or more threads can be executing code at the same time. The code is not in strict sense executed at the same time but rather the CPU time is being shared by the threads. Multitasking: The ability to perform a number of tasks (Sharing the time between different tasks) via multiple processing. Multi-Threading: Running more than one thread at the same time in the same program. Multiprocessing: Running more than one processor (true parallelism) Single thread implies one task at a time. C and C++ are single thread languages ( one activity at a time). BY GEORGE KOUTSOGIANNAKIS

3 3 Multi-Threading Multiprocessor machines can perform true concurrency. Java does timeslicing giving threads of equal priority small quantum of processor time that they can use in a round robin fashion. System calls refer to routines run by the operating system. The routine is called by a thread. In some operating systems only one thread at a time can make a system call whereas in others more that one thread can make system calls at the same time (example: “write this file to the disk”). Synchronization is the method used to ensure that multiple thraeds coordinate their activities so that one thread does not change data that another thread is working on. POSIX is a standard that describes how Operating systems should handle threads. BY GEORGE KOUTSOGIANNAKIS

4 4 Multi-Threading JAVA THREADS ARE IMPLEMENTED IN THE JVM. JAVA DOES NOT EXPOSE THE NATIVE (OPERATING SYSTEM) THREADS. THREFORE SOME ISSUES OF UNIFORMITY ACROSS PLATFORMS ARE RISEN. THRE ARE CURRENTLY NO ACCURATE BENCHMARKS OF JAVA’ S PARALLEL PROGRAMMING PERFORMANCE. BY GEORGE KOUTSOGIANNAKIS

5 5 Multi-Threading POSSIBLE PROBLEMS WITH THREADS: –RACE CONDITIONS OCCURS WHEN THE RESULTS OF A COMPUTATION ARE NONDETERMINISTIC DUE TO CONCURRENT EXECUTION –DEADLOCK- NO THREAD IS EXECUTED –STARVATION- A HIGHER PRIORITY THREAD PREVENTS INDEFINETELY LOWER PRIORITY THREADS FROM EXECUTING Synchronized methods have monitors. Monitors prevent more than one thread at a time to execute the synchronized method. BY GEORGE KOUTSOGIANNAKIS

6 6 Multi-Threading A thread ( process) can share the memory with other threads. Each thread needs its own stack. DATA CODE HEAP STACK BY GEORGE KOUTSOGIANNAKIS

7 7 Multi-Threading Definition: A thread is a single sequential flow of control within a program. BY GEORGE KOUTSOGIANNAKIS

8 8 Multi-Threading A single thread also has a beginning, a sequence, and an end. At any given time during the runtime of the thread, there is a single point of execution. However, a thread itself is not a program; it cannot run on its own. Rather, it runs within a program. As a sequential flow of control, a thread must carve out some of its own resources within a running program. (It must have its own execution stack and program counter for example.) The code running within the thread works only within that context. Thus, execution context is a synonym for thread. BY GEORGE KOUTSOGIANNAKIS

9 9 Multi-Threading Example of two threads running at the same time: Another program or another section of the same program BY GEORGE KOUTSOGIANNAKIS

10 10 Multi-Threading Once you know how to get a thread to do something, you need to understand the life cycle of a Thread A simplified version of the threads life is shown below: BY GEORGE KOUTSOGIANNAKIS

11 11 Multi-Threading The following classes are used with a thread: java.lang.Thread implements Runnable interface java.lang.ThreadGroup java.lang.ThreadDeath Constructors of Thread class: –public Thread() Allocates a new Thread object. –public Thread(Runnable target) target - the object whose run method is called. –public Thread(ThreadGroup group,Runnable target) group - the thread group, target - the object whose run method is called. –public Thread(String name) name - the name of the new thread. –public Thread(ThreadGroup group,String name) group - the thread group, name - the name of the new thread. BY GEORGE KOUTSOGIANNAKIS

12 12 Multi-Threading –public Thread(Runnable target, String name) target - the object whose run method is called, name - the name of the new thread. –public Thread(ThreadGroup group, Runnable target,String name) Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group. Some Important Methods: –public static native Thread currentThread() Returns a reference to the currently executing thread object. –public static native void yield() Causes the currently executing thread object to temporarily pause and allow other threads to execute. Most appropriate for non- timeslicing systems. –public static native void sleep(long millis) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors. BY GEORGE KOUTSOGIANNAKIS

13 13 Multi-Threading –public native synchronized void start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). – public void run() override and place the code that does the all the work for the thread in this method –public final void stop() Forces the thread to stop executing. The thread represented by this thread is forced to stop whatever it is doing abnormally and to throw a newly created ThreadDeath object as an exception. –public void interrupt() Interrupts this thread. –public void destroy() Destroys this thread, without any cleanup. –public final native boolean isAlive() Tests if this thread is alive. A thread is alive if it has been started and has not yet died. –public final void suspend() Suspends this thread. BY GEORGE KOUTSOGIANNAKIS

14 14 Multi-Threading – public final void resume() Resumes a suspended thread. – public final void setPriority(int newPriority) Changes the priority of this thread. Class java.lang.ThreadDeath –java.lang.Object – | – +----java.lang.Throwable – | – +----java.lang.Error – | – +----java.lang.ThreadDeath BY GEORGE KOUTSOGIANNAKIS

15 15 Multi-Threading public class ThreadDeath extends Error –An instance of ThreadDeath is thrown in the victim thread when the stop method with zero arguments in class Thread is called. Class java.lang.ThreadGroup –java.lang.Object – | – +----java.lang.ThreadGroup Thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every threadgroup except the initial thread group has a parent. A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups. BY GEORGE KOUTSOGIANNAKIS

16 16 Multi-Threading Creating a Thread The application in which an applet is running calls the applet's start method when the user visits the applet's page. The Clock applet creates a Thread, clockThread public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } –note that the the statement public void start() refers to the start method of the applet. BY GEORGE KOUTSOGIANNAKIS

17 17 Multi-Threading The start method of the Thread class (not of the Applet class) creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread's run method. ClockThread's run method is the one defined in the Clock class. –public void run() { – Thread myThread = Thread.currentThread(); – while (clockThread == myThread) { – repaint(); – try { – Thread.sleep(1000); – } catch (InterruptedException e){ – // the VM doesn't want us to sleep anymore, – // so get back to work – } BY GEORGE KOUTSOGIANNAKIS

18 18 Multi-Threading A THREAD IS A LIGHTWEIGHT ENTITY MADE OUT OF –REGISTERS –STACK –OTHER DATA –WHEN A THREAD CHANGES A PROCESS VARIABLE ALL OTHER THREADS SEE THAT CHANGE. Making a Thread Not Runnable –thread becomes Not Runnable A when one of these events occurs: Its sleep method is invoked. The thread calls the wait method to wait for a specific condition to be satisifed. The thread is blocking on I/O. BY GEORGE KOUTSOGIANNAKIS

19 19 Multi-Threading –If a thread has been put to sleep, then the specified number of milliseconds must elapse. – If a thread is waiting for a condition, then another object must notify the waiting thread of a change in condition by calling notify or notifyAll. –If a thread is blocked on I/O, then the I/O must complete. Stopping a thread –A program doesn't stop a thread like it stops an applet (by calling a method). Rather, a thread arranges for its own death by having a run method that terminates naturally. For example, the while loop in this run method is a finite loop public void run() { int i = 0; while (i < 100) { i++; System.out.println("i = " + i); } BY GEORGE KOUTSOGIANNAKIS

20 20 Multi-Threading –the exit condition for this run method is the exit condition for the while loop because there is no code after the while loop: while (clockThread == myThread) { –This condition indicates that the loop will exit when the currently exiting thread is not equal to clockThread. When you leave the page, the application in which the applet is running calls the applet's stop method. This method then sets the clockThread to null, thereby telling the main loop in the run method to terminate: – public void stop() { // applets' stop method – clockThread = null; – } – If you revisit the page, the start method is called again and the clock starts up again with a new thread. Even if you stop and start the applet faster than one iteration of the loop, clockThread will be a different thread than myThread and the loop will still terminate. –You can create at least 10 thread priorities in Java. BY GEORGE KOUTSOGIANNAKIS

21 21 Multi-Threading Notice that min priority is 1 and maximum is 10 Threads are Operating system dependent. –Solaris executes until its completion. Threads of equal priority wait. A thread of higher priority can interrupt. –Windows does timeslicing between threads of equal priority. Yield applies only to threads of equal priority. If a higher priority thread is ready the current thread preempties. Yield does not apply to timeslicing. BY GEORGE KOUTSOGIANNAKIS

22 22 Multi-Threading LIFECYCLE OF THREADS BY GEORGE KOUTSOGIANNAKIS THREAD IS BORN START() READY STATE EQUAL PRIORITY THREADS READY STATE HIGHER PRIORITY THREAD RUNNABLE STATE RUNNING STATE CURRENT THREAD PREEMPTIES CURRENT THREAD YIELDS RUN COMPLTED DEAD STATE EXCEPTION DEAD STATE BLOCKED STATE SLEEPING STATE WAIT FOR OBJECT “NOTIFY OR NOTIFY ALL” BY ANOTHER THREAD THAT CONTROLS THE OBJECT

23 23 Multi-Threading SYNCHRONIZATION OF TRHEADS -assume time t1 (an instance in time).Threads 1 and 2 attempt to invoke methods 1 and 2 RESPECTIVELY using object 1 Object 1 Monitor Monitor assures that only one method at a time is active on the object -obtains lock on the object- gives lock to thread 1 (depending on priority) Synchronized method 1 Synchronized method 2 Synchronized method 3 thread1thread2 Only thread1 executes method 2 at time t1-thread 2 goes to wait state. BY GEORGE KOUTSOGIANNAKIS

24 24 Multi-Threading –When thread 1 completes the object notifies thread 2 to become ready again and attempt to obtain the lock on the object. –If more than one thread is waiting then it is possible to notifyAll to go to the ready state. –Sections of code that must be syncronized and try to be executed simultaneously are called: critical sections –The term synchronized is a modifier that can be used with methods (and some times with instances-objects). BY GEORGE KOUTSOGIANNAKIS

25 25 Multi-Threading CONSUMER PRODUCER PROBLEM (CPP) PROBLEM DEFINITION: Under ideal conditions the following applies:  There exists a Buffer with a finite number of items or slots all of which are identical.  There is a Producer that produces the items into the slots, one item at a time.  There is a Consumer that consumes the items from the slots, one at a time.  The Producer takes a finite amount of time to produce each item.  The Consumer takes a finite amount of time to consume each item. BY GEORGE KOUTSOGIANNAKIS

26 26 Multi-Threading  Use of a Buffer Slot is mutually exclusive, that is, the producer cannot produce into and the consumer cannot consume from the same slot at the same time.  Producer can produce only into an empty slot, that is, it cannot produce into a slot already containing an unconsummated item.  Consumer must consume an item just once, that is, it cannot consume again an item it has already consumed in the previous iteration. –Trouble arises when the Producer wants to put a new item in the buffer, but it is already full. – The solution is for the Producer to go to sleep, to be awakened when the consumer has removed one or more items. BY GEORGE KOUTSOGIANNAKIS

27 27 Multi-Threading –Similarly, if the Consumer wants to remove an item from the buffer and sees that the buffer is empty, it goes to sleep until the Producer puts something in the buffer and wakes it up. –The idea is to try to keep them both satisfied. Neither the producer nor the consumer should have to go to sleep. Consumer process buffer Producer process

28 28 Multi-Threading –Trouble also arises if some of the rules established are not held. PCP W/O SYNCH. –The CPP can loose the contents of the buffer if there in no synchronization. –This can result in the producer trying to enter data into the buffer before the consumer reads what is there already ( Producer is faster than the consumer). –Or the opposite can happen. The consumer can be reading the same data twice if the producer did not have a chance to update the buffer location (consumer is too fast) –therefore access to shared data by concurrent threads must be carefully controlled otherwise incorrect results can occur. BY GEORGE KOUTSOGIANNAKIS

29 29 Multi-Threading EXAMPLE: SharedShell.java (15.5) sharedInt setSharedInt method getSharedInt These two methods do not synchronize access to variable sharedInt BY GEORGE KOUTSOGIANNAKIS

30 30 Multi-Threading –Object h of HoldInteger class is the shared object –Objects p and c are the producer/consumer threads. They have been instantiated with the constructors of the corresponding producer and consumer classes and they both reference h. –h gets passed as an argument to the ProduceInteger class, which creates another instance variable (pHold) that references h. –10 values are passed to the setSharedInt method between randomly generated sleeps (range 0-3 sec). –Class consumeInteger creates instance variable cHold that also refers to h –The getSharedInt method is called between random sleeps of the consumer thread. This continues as long as there is data available. –The problem is that the values produced are not always consumed and that sometimes the same value is consumed twice. BY GEORGE KOUTSOGIANNAKIS

31 31 Multi-Threading The problem can be solved by synchronizing the setSharedInt and getSharedInt methods. Example SharedCell.java with synchronization. sharedInt setSharedInt method getSharedInt These two methods are synchronized using the boolean writeable Witeable=trueWriteable=false BY GEORGE KOUTSOGIANNAKIS

32 32 Multi-Threading Acquisition of lock/release of lock by producer object Producer Object pHold Method setSharedInt invocation Lock HoldInteger object h If !writeable=true (writeable=false) wait Release lock While loop This loop continues until pHold is notified that writeable= true -then setSharedInt changes the value of sharedInt BY GEORGE KOUTSOGIANNAKIS

33 33 Multi-Threading –After the sharedInt changed value -writeable=false and notify is invoked –This causes the getSharedInt to get out of the wait state and return the sharedInt value. –The cHold object works the same way. A circular buffer allows the producer to continue producing and deposit the production in a buffer. Daemon Threads are threads that run for the benefit of other threads ( example is the garbage collector) Runnable Interface –used when we want to implement multithreading in a class that extends a class other than Thread. BY GEORGE KOUTSOGIANNAKIS

34 34 Multi-Threading –Code that controls the thread is in run() method. –Thread is created by using the constructor: public Thread(Runnable runnableobject) or by implementing the Runnable interface – First write a class that implements the interface –public MyRunnable implements Runnable { public void run() { //code } } –You can instantiate objects of the new class and use them to control threads i.e. Runnable r= new MyRunnable –Two reasons for using Runnable: we are not changing the nature of the thread itself. We are only changing the run() method. BY GEORGE KOUTSOGIANNAKIS

35 35 Multi-Threading We can subclass something else –Runnable can be the work to be done while the thread is the engine to do the work. –Remember that by default the run() method of Thread class lloks for a Runnable and calls its run() method. Thread Groups –Reasons for grouping Threads: Organizing threads according to their tasks Suspending or resuming or stopping all the threads in a group at the same time. BY GEORGE KOUTSOGIANNAKIS

36 36 Multi-Threading –Use constructors public ThreadGroup(String nameOfThreadGroup) or public ThreadGroup(ThreadGroup nameOfParentThread, String nameOfChildThread) BY GEORGE KOUTSOGIANNAKIS


Download ppt "1 Handout 10 MULTI-THREADING BY GEORGE KOUTSOGIANNAKIS THIS DOCUMENT CAN NOT BE REPRODUCED OR DISTRIBUTED WITHOUT TH E WRITTEN PERMISSION OF THE AUTHOR."

Similar presentations


Ads by Google