Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.

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

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Li Tak Sing (Lecture 3) COMPS311F 1. Flexible synchronization with locks Synchronized methods and statement blocks: Advantages: Relatively easy to use.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Practice Session 7 Synchronization Liveness Guarded Methods Model
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
ThreadThread Thread Basics Thread Synchronization Animations.
Threads Just Java: C10–pages 251- C11–pages 275-
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Internet Software Development More stuff on Threads Paul Krause.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Threads some important concepts Simon Lynch
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Threading Eriq Muhammad Adams J
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
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.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
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.
Multithreading and Garbage Collection Session 16.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Thread Programming
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Chapter 13: Multithreading
Multi Threading.
Multithreading.
Java Programming Language
Multithreading in Java
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Cancellation.
Condition Variables and Producer/Consumer
Multithreading.
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Software Engineering and Architecture
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Li Tak Sing COMPS311F

Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs with only one thread. In other words, these single threaded programs would only do one thing at a time. A program with many threads would be one that can do many things at the same time. For example, it can read a file, do some calculation at the same time.

Threads When you run a Java program, a thread is used to execute statements in the main method.

Threads However, there are other threads that you may not aware. For example, if your program has a button and you have added an ActionListener, then there is a thread that is going to execute some statements when you press the button.

Threads In all Java programs, there is a thread to collect garbage. So although you think that you are writing a single threaded Java program, there are more than one thread in it.

Life cycle of a thread

Thread Some threads are created by the Java runtime system. For example, the garbage collection thread, the thread that executes the statements of the main method, the thread that handle listeners. However, you can also create your own threads.

Threads Thread is a Java class that represents a thread. When you create a thread using the statement: Thread thread=new Thread(); it enter the Born state in the last figure.

Thread When the start method of a thread is invoked, it enters the Ready state. When the a CPU is assigned to the Thread, it enters the Running state and the statements defined in the run method of the thread would be executed. When the time allocated for the thread has finished, the thread goes back to Ready state.

Thread When the static sleep method of Thread is executed, the current thread would go to the Sleep state and the CPU would stop executing the statements in the run method. When the prescribed time for stopping has expired, the thread would go back to the ready state.

Thread When the thread is executing wait method of an object, it enters the wait state and it will stop exection. When some other thread invoke the notify or notifyAll method of the object, the thread would go back to the ready state.

Thread When the thread is trying to do some IO but is blocked, it enter the Blocked state. When the IO is finally finished, it goes back to the ready state again. When the thread has finished executing all the statements in the run method, it enters the Death state.

Methods of Thread public void run(): The statements in this method would be executed when the thread is in the Running state. You would not invoke this method directly. The method would be invoked when you invoke the start method. The original run() method of Thread contains nothing. So you need to override this method to whatever you want this thread to do for you.

Methods of Thread public void start(): This method is used to change a thread from the Born state to the Ready state. Then, if a CPU is available, the statements in the run() method would be executed.

public class TestThread extends Thread { /** Creates a new instance of TestThread */ public void run() { for (int i=0;i<10;i++) { double sum=0; for (int j=0;j<1000;j++) { for (int k=0;k<1000;k++) { sum+=Math.sin(i+j+k); } System.out.println("thread: "+sum); }

public static void main(String st[]) { TestThread aThread=new TestThread(); //thread enters the Born state aThread.start(); //thread enters the Ready state. for (int i=0;i<10;i++) { double sum=0; for (int j=0;j<1000;j++) { for (int k=0;k<1000;k++) { sum+=Math.sin(i+j+k); } System.out.println("main: "+sum); } }

main: thread: main: main: thread: main: thread: main: main: thread: main: main: thread: main: thread: main: thread: thread: thread: thread:

Thread Interesting observations in last program: the run method has never been called in the program. However, we can see that the statements in the run method have been executed. the statements in the run method are interleaved with statements the main method. It is a proof that there are two flows of control, or two threads.

currentThread In last example, you actually encounter two threads, one is created by the programmer and is called aThread in the program. The other thread is the thread that executes the statements in the main method.

currentThread Since you have the reference aThread refers to the first thread, you can do whatever you want to aThread directly. However, how can you do something about the thread that executes the main method? The answer is the static method currentThread of Thread.

currentThread In anywhere in your program, if you execute the statement: Thread cThread=Thread.currentThread(); then cThread refers to the thread that is currently executing this statement.

sleep sleep is a static method of Thread. When you execute this method, the current thread would go to sleep for certain period. The method is a static method which means that you can only ask yourself to sleep, not others.

sleep Now, assume that threadA is executing the statement threadB.sleep(1000) and threadB is executing the print statement. threadA:..... threadB.sleep(1000); threadB:..... System.out.printf("abcd");.....

sleep Which thread would go to sleep? Although we have executed the sleep statement in the form: threadB.sleep(1000); However, sleep is a static method, therefore it will not affect threadB. In fact, this method will only affect the current thread. Now, this statement is executed by threadA, therefore it is threadA that is going to sleep, not threadB.

sleep So actually, the statement: threadB.sleep(1000); is better replaced by Thread.sleep(1000); to avoid misunderstanding. In some textbooks, they write statement like this: Thread.currentThread().sleep(1000); which is again not necessary.

sleep the definition of the sleep method is: public static void sleep(long millis) throws InterruptedExceptionInterruptedException The method would throw InterruptedException when some other threads interrupt this sleeping thread.

interrupt Unlike sleep which is a static method, interrupt is a non- static method of Thread. This means that you need to get hold of a thread before you can interrupt it.

interrupt Assume that a thread A is interrupting a thread B. If thread B is sleeping, then, the InterruptedException would be thrown to thread B. So control of thread B will be passed to the exception handler. Note that thread A would not receive any exception.

Thread A:..... threadB.interrupt();..... Thread B:.... try { Thread.sleep(10000); } catch (InterruptedException e) {..... } Assume that Thread B is now sleeping at the Thread.sleep(10000) statement and Thread A is now executing the threadB.interrupt() statement. Then, Thread B would then throw an InterruptedException and control of Thread B would go the catch block. However, Thread A would not throw any exception and control would flow to the statement after threadB.interrupt().

Interrupting a thread Note that when thread A interrupts thread B but thread B is not sleeping or waiting, then thread B will not be affected in any way except that it has an internal flag to record that someone has interrupt it. There are two ways to check that a thread has been interrupted.

interrupted public static boolean interrupted(): again this method is a static method which means that we can only use it to check whether the current thread has been interrupted. After this call, the internal flag would be set to false again so the second call to this method would return false unless the thread is interrupted again.

isInterrupted public boolean isInterrupted(): note that this method is not static which means that we can check whether a particular thread has been interrupted or not, not only the current thread. The internal flag that record whether the thread has been interrupted would not be changed after this call.

threadA:..... threadB.interrupt();..... if (threadB.isInterrupted()) {... }... if (threadB.isInterrupted()) {... }... threadB:.... System.out.println("hello"); if (Thread.interrupted()) { } Assume that threadB is executing the print statement when threadA interrupts it. threadB would not be affected in any way except that it has an internal flag to record that it has been interrupted. Then, threadA execute the threadB.isInterrupted() method to check whether threadB has been interrupted. The returned value of the method is true and therefore the statement in the if block would be executed.

interrupt and interrupted Then, threadB executes the statement Thread.interrupted() to check whether itself has been interrupted. Again, the returned value is true and therefore the statements in the if block would be executed. However, the internal flag would be set to false again. Then, threadA executes the statement threadB.isInterrupted() again. This time, the returned value is false.

Synchronization When more than one threads are making change to an object problems may occur.

class TestObject { public int a=0; public void addTwo(int id) { int b=a; System.out.println("Thread"+id+":the value of b is "+b); try {Thread.sleep(1000); } catch (Exception e) {} a=b+2; System.out.println("Thread"+id+":the value of a is "+a); } public class TestThread extends Thread { TestObject object; int id; public TestThread(TestObject obj,int id) {

object=obj; this.id=id; } public void run() { for (int i=0;i<4;i++) { object.addTwo(id); } public static void main(String st[]) { TestObject obj=new TestObject(); TestThread thread1=new TestThread(obj,1); TestThread thread2=new TestThread(obj,2); thread1.start(); thread2.start(); }

Thread1:the value of b is 0 Thread2:the value of b is 0 Thread1:the value of a is 2 Thread1:the value of b is 2 Thread2:the value of a is 2 Thread2:the value of b is 2 Thread1:the value of a is 4 Thread1:the value of b is 4 Thread2:the value of a is 4 Thread2:the value of b is 4 Thread1:the value of a is 6 Thread1:the value of b is 6 Thread2:the value of a is 6 Thread2:the value of b is 6 Thread1:the value of a is 8 Thread2:the value of a is 8

synchronized The problem of last program is that two threads are trying to change the state of an object at the same time and they interfere with each other.

MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

Synchronized The synchronized keyword is used to make sure that a resource can only be accessed by one thread. When a method of an object is declared as synchronized, then, when a thread wants to invoke the method, it must secure a lock on the object first. Only one thread can secure the lock on the object at anytime.

Synchronized method So if an object has a number of synchronized methods and if a thread has secured a lock and is invoking one of the synchronized method, then no other threads can invoke any of the synchronized methods of the object. When a thread has secured a lock on an object, we say that the thread owns the monitor of the object.

Waiting for securing a lock of an object If a thread has secured a lock on an object, then all other threads who also want to secure the lock has to wait. Note that this wait is not the same as the wait state which we will mentioned later. When the thread that has secured the lock of an object finally releases the lock, then one of the threads that are waiting for the lock can now success in securing the lock and proceed.

class TestObject { public int a=0; synchronized public void addTwo(int id) { int b=a; System.out.println("Thread"+id+":the value of b is "+b); try {Thread.sleep(1000); } catch (Exception e) {} a=b+2; System.out.println("Thread"+id+":the value of a is "+a); } public class TestThread extends Thread { TestObject object; int id; public TestThread(TestObject obj,int id) {

object=obj; this.id=id; } public void run() { for (int i=0;i<4;i++) { object.addTwo(id); } public static void main(String st[]) { TestObject obj=new TestObject(); TestThread thread1=new TestThread(obj,1); TestThread thread2=new TestThread(obj,2); thread1.start(); thread2.start(); }

Thread1:the value of b is 0 Thread1:the value of a is 2 Thread2:the value of b is 2 Thread2:the value of a is 4 Thread1:the value of b is 4 Thread1:the value of a is 6 Thread2:the value of b is 6 Thread2:the value of a is 8 Thread1:the value of b is 8 Thread1:the value of a is 10 Thread2:the value of b is 10 Thread2:the value of a is 12 Thread1:the value of b is 12 Thread1:the value of a is 14 Thread2:the value of b is 14 Thread2:the value of a is 16

Buffer example Consider a fixed size buffer of integers example which allows some producers to put integers to it and allows some consumers to get integers from it. The integers are removed in the first-in-last-out order. That is, it operates like a stack. Let's assume that there are two methods called get and put of the buffer which allows consumers and producers to get and put integers respectively.

Buffer example Since there are many consumers and many producers, the two methods must be declared as synchronized. So the two methods should declare like this: synchronized public void put(int i) {...} synchronized public int get() {....}

Buffer example We need to solve two problems: what should we do if we want to get an integer from the buffer but the buffer is empty? what should we do if we want to put an integer to the buffer but the buffer is full? One solution is to use exception. That is, the methods will throw an exception whenever the above two situations happen.

public class Buffer { private int elem[]=new int[10]; private int noElems; /** Creates a new instance of Buffer */ synchronized public void put(int i) throws Exception { if (noElems==10) throw new Exception("buffer is full"); elem[noElems++]=i; } synchronized public int get() throws Exception { if (noElems==0) throw new Exception("buffer is empty"); return elem[--noElems]; } }

Buffer example The problem of last example is that if a consumer really need to put an integer to the buffer, it must do it using a loop because the first attempt may not be successful.

Buffer example Code for producer:..... while (true) { try { buffer.put(i); break; } catch (Exception e) { }

Buffer example code for consumer:..... while (true) { try { i=buffer.get(); break; } catch (Exception e) { }

Buffer example So you can see that the code for using the buffer is very complicated. We can solve this problem by using the wait method and notify or notifyAll methods.

wait, notify, notifyAll All of these three are methods of Object. Since all Java classes are subclasses of Object, this means that all classes have these three methods. Before a thread can invoke one of these three methods of an object, the thread must own the monitor of the object first. Otherwise an exception would be thrown.

Wait When a thread invokes the wait method of an object, the thread will go to the wait state and release the monitor of the object. When a thread is in the wait state and another thread interrupts the former, an InterruptedException would be thrown to the former. Then if there are threads wanting to acquire the monitor, one of them would successfully do so.

notify Assume that a thread A has invoked the wait method of an object, and another thread B has acquired the monitor of the object. Now, thread B invoke the notify method of the object, then, one of the threads that have invoked the wait method of the object will go back to the ready mode. However, the thread will try to acquire the monitor of the object first before it can proceed.

NotifyAll This is similar to the notify method except that all threads which are waiting at the object would be going back to the ready state.

notify or notifyAll? So which one should be used? If you wake up a thread and then the thread cannot go ahead, there is a possibility that the thread would go to wait again. Then we may have all the threads waiting and therefore we end up with a deadlock.

notify or notifyAll So using notifyAll is a safe option.

public class Buffer2 { private int elem[]=new int[10]; private int noElems; /** Creates a new instance of Buffer */ synchronized public void put(int i) { while (noElems==10) { try { wait();} catch (Exception e) {} } elem[noElems++]=i; notifyAll(); } synchronized public int get() throws Exception { while (noElems==0) { try { wait();} catch (Exception e) {} } notifyAll(); return elem[--noElems]; }

wait, notify, notifyAll Note that the code that use this method is now: buffer.put(3); or int i=buffer.get(); So you can see that the code is much simpler than the one you see when exception method is used.

wait, notify, notifyAll Note that when a thread executes the wait method of an object, it will release the monitor of the object.

wait, notify, notifyAll If the thread owns n monitors and it execute the wait method of an object, it will still have (n-1) monitors. When another thread has finished with object and invoke the notify or notifyAll method, then one thread or all threads will go back to the ready mode. But only one can acquire the monitor and proceed.

Runnable Runnable is an interface which has only one method which is: public void run(); Runnable is not a thread but we can use it to create a thread. There is a constructor of Thread which accepts a Runnable as parameter. When you start the thread, the run method of the Runnable would be executed.

Runnable public class MyRunnable implements Runnable { private String st; /** Creates a new instance of MyRunnable */ public MyRunnable(String st) { this.st=st; } public void run() { for (int i=0;i<10;i++) { try { Thread.sleep(1000); } catch (Exception e) {} System.out.println(st); }

Runnable } public static void main(String st[]) { MyRunnable r1=new MyRunnable("hello"); MyRunnable r2=new MyRunnable("world"); Thread th1=new Thread(r1); Thread th2=new Thread(r2); th1.start(); th2.start(); } }

synchronized keyword We have used the synchronized to declared a method can only be invoked by one thread at a time. In fact, when you have successfully acquire the monitor of an object by invoking a synchronized method of the object, no other threads can invoke any synchronized methods of the object.

synchronized There is another of acquiring the monitor of an object: synchronized (object) {.... // the current thread has acquired the // monitor of object. }

synchronized But this method is not to be advised. It is much better to use synchronized methods instead of using the synchronized keyword on an object.