1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

Slides:



Advertisements
Similar presentations
1 Race Conditions When threads share access to a common object/data, they can conflict with each other and mess up the consistency of the object/data.
Advertisements

1Deadlock DeadlockedBankAccount –withdraw(){ lock.lock(); while(balance
Multithreading Horstmann ch.9. Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library.
Chapter 9 (Horstmann’s Book) Multithreading Hwajung Lee.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1Threads What are they? Why are they important? How are they implemented in OSes? How to use threads? (in Java)
Concurrency…leading up to writing a web crawler. Web crawlers.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
More Multithreaded Programming in Java David Meredith Aalborg University.
50.003: Elements of Software Construction Week 5 Basics of Threads.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Multithreading. Chapter Goals To understand how multiple threads can execute in parallel To learn how to implement threads To understand race conditions.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Next week (July 21) –No class –No office hour. Problem Multiple tasks for computer –Draw & display images on screen –Check keyboard & mouse input –Send.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
Threads.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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
Timers, Threads, and Concurrency
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Java Thread and Memory Model
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
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.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
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.
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 Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Java Multithreading.
Multithreading.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Java Concurrency.
Multithreading Chapter 9.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Java Concurrency.
Condition Variables and Producer/Consumer
Multithreading.
Cancellation.
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreaded Programming
برنامه‌نویسی چندنخی Multi-Thread Programming
Chapter 9 Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2); thread1.start(); thread2.start(); –Do this: runnable1.run(); runnable2.run(); Describe the result of the modified program and explain why the result looks like that (why it is different from the result of the original Code 2).

2 Thread State Runnable Blocked new New start() I/O op completion or thread sync done I/O operation or wait for thread sync (lock) Terminated Exits run() or Explicit thread termination Waiting sleep() join() wait() await() notify() notifyAll() signal() signalAll() interruption Timed Waiting sleep() join() wait() await() notify() notifyAll() signal() signalAll() interruption

3 New –A Thread object is created. start() has not called on the object yet. Runnable –Java does not distinguish runnable and running. A running thread is still in the Runnable state. Dead –A thread automatically dies when run() returns.

4 public class Thread { public enum State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED } public Thread.State getState() public boolean isAlive() Alive –in the Runnable, Blocked, Waiting or Timed Waiting state. –isAlive() is usually used to poll a thread to see if it is terminated.

5GreetingThreadRunner.java Output: –NEW –false –RUNNABLE –true –Wed Mar 28 04:25:01 EDT 2007 Goodbye World –Wed Mar 28 04:25:01 EDT 2007 Hello World –Wed Mar 28 04:25:02 EDT 2007 Hello World –Wed Mar 28 04:25:02 EDT 2007 Goodbye World –Wed Mar 28 04:25:03 EDT 2007 Hello World –Wed Mar 28 04:25:03 EDT 2007 Goodbye World –…… –TERMINATED –false

6 Thread Termination Implicit termination –A thread triggers its own death when run() returns. Once a thread starts executing run(), it continues execution until it returns. Explicit termination –A thread is terminated by another thread. when a certain condition is met. –e.g. web browser –Two ways Setting a flag Interrupting a thread

7 Thread Termination Flag Stop a thread by setting a flag to signal that the thread should stop. The thread periodically checks out the flag to determine if it should exit.

8SummationRunnable.java

9 Main threadThread t t.start() Printing 10 Sleeps for 1 sec t.setDone() Prints “stopped by main()” Prints #s t.join() Waits for t.run() to return. Goes to the Waiting state. t.start() Printing 9, 8, 7, …

10 Output: –# –10 –# –stopped by main()! –9 –8 –7 –6 –5 –4 –3 –2 –1 –0 –print done

11 Thread Interruption Interrupt a thread to minimize this delay. –Signals a thread that the thread should exit. –Thread.interrupt()

12InterruptableTask.java

13InterruptableTask.java Main threadThread t t.start() Printing 1 Sleeps for 1 sec Goes to the Waiting state. t.interrupt() Interrupted! Goes to the Runnable state. Catches InterruptedException A try-catch clause is always necessary to catch InterruptedException, when using sleep().

14InterruptableTask2.java

15InterruptableTask2.java Main threadThread t t.start() Keep printing 1s t.interrupt() Interrupted! Prints 4 Sleeps for 2 sec Goes to the Waiting state. Interrupt() does not stop a target thread.

16SummationRunnableInterruptable.java

17 Main threadThread t t.start() Prints 10 t.interrupt() Prints #s t.join() Waits for t.run() to return. Goes to the Waiting state. t.start() Printing 9, 8, 7, … Sleeps for 1 sec Goes to the Waiting state. Interrupted! Goes to the Runnable state. Catches InterruptedException

18 Output –# –10 –# –interrupted by main()! –9 –8 –7 –6 –5 –4 –3 –2 –1 –0 –print done

19 SummationRunnable.java and SummationRunnableInterruptable.java t.start() Prints 10 t.interrupt() t.join() t.start() Printing 9, 8, 7, … Sleeps for 1 sec Goes to the Waiting state. Interrupted! Goes to the Runnable state. Catches InterruptedException t.start() Printing 10 Sleeps for 1 sec t.setDone() Prints “stopped by main()” t.join() t.start() Printing 9, 8, 7, …

20 Stack and Frames User stack (Java stack) –One stack per thread –A set of blocks called frames. Frame –associated with a method call. –Pushed when a method is called. –Poped when a method returns. Multi-threaded process TCB U K Stacks U ProgU dataOS Res thread TCB U K Stacks thread PCB TCB U K Stacks thread – Contains –a table of local variables of the associated method –an operand stack containing the values of the partial results of the method –Program counter that points to the Java bytecode instruction currently being executed.

21 public class Thread { static void dumpStack() public StackTraceElement[] getStackTrace() public StackTraceElement[] getAllStackTrace()

22InterruptableTask3.java Output: –1 –3 –4 –java.lang.Exception: Stack trace –at java.lang.Thread.dumpStack(Thread.java:1176) –at cs681.threads.InterruptableTask3.run(InterruptableTask3.java:17) –at java.lang.Thread.run(Thread.java:613)