Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);
Software Engineering Oct-01 #11: All About Threads Phil Gross.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
ThreadThread Thread Basics Thread Synchronization Animations.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Java Threads CS Introduction to Operating Systems.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Java Programming: Advanced Topics
1 Java Threads Instructor: Mainak Chaudhuri
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
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.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
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.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Next week (July 21) –No class –No office hour. Problem Multiple tasks for computer –Draw & display images on screen –Check keyboard & mouse input –Send.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
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
Java Thread and Memory Model
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.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Java 3: Odds & Ends Advanced Programming Techniques.
Multi-Threading in Java
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
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.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
Multithreading.
Multithreaded Programming in Java
Multithreading Chapter 9.
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Java Thread.
Multithreading in java.
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1

Quick Review  What are the two approaches to creating Java threads? Advanced Topics in Software Engineering 2

Advanced Topics in Software Engineering 3 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo

Advanced Topics in Software Engineering 4 Thread Creation There are two ways to create a thread in Java:  Extend the Thread class  Implement the Runnable interface

Advanced Topics in Software Engineering 5 The Thread class class A extends Thread { public A (String name) { super (name); } public void run () { System.out.println(“My name is ” + getName()); } class B { public static void main (String [] args) { A a = new A (“mud”); a.start (); }

Advanced Topics in Software Engineering 6 The Runnable interface class A extends... implements Runnable { public void run () { System.out.println(“My name is ” + getName () ); } class B { public static void main (String [] args) { A a = new A (); Thread t = new Thread (a, “mud, too”); t.start (); }

Advanced Topics in Software Engineering 7 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo

Advanced Topics in Software Engineering 8 Lock Each Java object is implicitly associated with a lock. To invoke a synchronized method of an object, a thread must obtain the lock associated with this object. The lock is not released until the execution of the method completes. The locking mechanism ensures that at any given time, at most one thread can execute any synchronized method of an object. Important: Lock is per object (NOT per method)!

Advanced Topics in Software Engineering 9 wait, notify and notifyAll The execution of wait on an object causes the current thread to wait until some other thread to call notify or notifyAll. A thread must own the object lock before it invokes wait on an object. The execution of wait will also release the lock. When a waiting thread is notified, it has to compete and reacquire the object lock before it continues execution. What’s the difference between notify and notifyAll?

Advanced Topics in Software Engineering 10 join A thread t1 can wait until another thread t2 to terminate. t1 t2 t2.join () end

Advanced Topics in Software Engineering 11 interrupt interrupt allows one thread to send a signal to another thread. It will set the thread’s interrupt status flag, and will throw a ThreadInterrupted exception if necessary. The receiver thread can check the status flag or catch the exception, and then take appropriate actions.

Advanced Topics in Software Engineering 12 Other Thread methods Method sleep puts the running thread into sleep, releasing the CPU. Method suspend suspends the execution of a thread, which can be resumed later by another thread using method resume. Method stop ends the execution of a thread. Note that suspend, resume, and stop has been deprecated in Java 2. (For more info, refer to

Advanced Topics in Software Engineering 13 Daemon Thread A daemon thread is used to perform some services (e.g. cleanup) for other threads. Any thread can be marked as a daemon thread using setDaemon (true). A program terminates when all its non-daemon threads terminate, meaning that daemon threads die when all non-daemon threads die.

Advanced Topics in Software Engineering 14 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo

Advanced Topics in Software Engineering 15 State Transition Diagram suspend blocked newrunnablerunning suspendedblocked dead startscheduled CS, yield IO, sleep wait, join stop suspend stop, run ends suspend resume stop IO completes, sleep expires, notify, notifyAll, join completes IO completes stop suspend stop resume

Advanced Topics in Software Engineering 16 Scheduling In general, there are two types of scheduling: non- preemptive scheduling, and preemptive scheduling. In non-preemptive scheduling, a thread runs until it terminates, stops, blocks, suspends, or yields. In preemptive scheduling, even if the current thread is still running, a context switch will occur when its time slice is used up.

Advanced Topics in Software Engineering 17 Priorities Each thread has a priority that also affects their scheduling to run. If a thread of a higher priority enters the runnable set, the currently running thread will be preempted by the new thread.

Advanced Topics in Software Engineering 18 Java Threads  Thread Creation  Thread Synchronization  Thread States And Scheduling  Short Demo