Java Threads CS 537 - Introduction to Operating Systems.

Slides:



Advertisements
Similar presentations
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Advertisements

Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
1 Java Threads Instructor: Mainak Chaudhuri
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)
1 Some Salient Characteristics of Java Java is platform independent: the same program can run on any correctly implemented Java system Java is object-oriented:
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
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.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
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.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
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.
1 Web Based Programming Section 8 James King 12 August 2003.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
1 Multithreaded Programming using Java Threads Prof. Rajkumar Buyya Cloud Computing and Distributed Systems (CLOUDS) Laboratory Dept. of Computer Science.
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.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
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
Threading and Concurrency COM379T John Murray –
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
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.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreaded Programming in Java
Multithreading in Java
Threads Chate Patanothai.
Multithreaded Programming in Java
Multithreading.
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Java Threads (Outline)
Java Threads (Outline)
21 Threads.
Java Thread.
Multithreading in java.
Threads and Multithreading
Concurrency, Processes and Threads
Lecture 19 Threads CSE /6/2019.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Threads CS Introduction to Operating Systems

What is a Thread? Individual and separate unit of execution that is part of a process –multiple threads can work together to accomplish a common goal Video Game example –one thread for graphics –one thread for user interaction –one thread for networking

What is a Thread? video interaction networking Video Game Process

Advantages easier to program –1 thread per task can provide better performance –thread only runs when needed –no polling to decide what to do multiple threads can share resources utilize multiple processors if available

Disadvantage multiple threads can lead to deadlock –much more on this later overhead of switching between threads

Creating Threads (method 1) extending the Thread class –must implement the run() method –thread ends when run() method finishes –call.start() to get the thread ready to run

Creating Threads Example 1 class Output extends Thread { private String toSay; public Output(String st) { toSay = st; } public void run() { try { for(;;) { System.out.println(toSay); sleep(1000); } } catch(InterruptedException e) { System.out.println(e); }

Example 1 (continued) class Program { public static void main(String [] args) { Output thr1 = new Output(“Hello”); Output thr2 = new Output(“There”); thr1.start(); thr2.start(); } main thread is just another thread (happens to start first) main thread can end before the others do any thread can spawn more threads

Creating Threads (method 2) implementing Runnable interface –virtually identical to extending Thread class –must still define the run()method –setting up the threads is slightly different

Creating Threads Example 2 class Output implements Runnable { private String toSay; public Output(String st) { toSay = st; } public void run() { try { for(;;) { System.out.println(toSay); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println(e); }

Example 2 (continued) class Program { public static void main(String [] args) { Output out1 = new Output(“Hello”); Output out2 = new Output(“There”); Thread thr1 = new Thread(out1); Thread thr2 = new Thread(out2); thr1.start(); thr2.start(); } main is a bit more complex everything else identical for the most part

Advantage of Using Runnable remember - can only extend one class implementing runnable allows class to extend something else

Controlling Java Threads _.start(): begins a thread running wait() and notify(): for synchronization –more on this later _.stop(): kills a specific thread (deprecated) _.suspend() and resume(): deprecated _.join(): wait for specific thread to finish _.setPriority(): 0 to 10 (MIN_PRIORITY to MAX_PRIORITY); 5 is default (NORM_PRIORITY)

Java Thread Scheduling highest priority thread runs –if more than one, arbitrary yield(): current thread gives up processor so another of equal priority can run –if none of equal priority, it runs again sleep(msec): stop executing for set time –lower priority thread can run

States of Java Threads 4 separate states –new: just created but not started –runnable: created, started, and able to run –blocked: created and started but unable to run because it is waiting for some event to occur –dead: thread has finished or been stopped

States of Java Threads new runnable blocked dead start() stop(), end of run method wait(), I/O request, suspend() notify(), I/O completion, resume()

Java Thread Example 1 class Job implements Runnable { private static Thread [] jobs = new Thread[4]; private int threadID; public Job(int ID) { threadID = ID; } public void run() { do something } public static void main(String [] args) { for(int i=0; i<jobs.length; i++) { jobs[i] = new Thread(new Job(i)); jobs[i].start(); } try { for(int i=0; i<jobs.length; i++) { jobs[i].join(); } } catch(InterruptedException e) { System.out.println(e); } }

Java Thread Example 2 class Schedule implements Runnable { private static Thread [] jobs = new Thread[4]; private int threadID; public Schedule(int ID) { threadID = ID; } public void run() { do something } public static void main(String [] args) { int nextThread = 0; setPriority(Thread.MAX_PRIORITY); for(int i=0; i<jobs.length; i++) { jobs[i] = new Thread(new Job(i)); jobs[i].setPriority(Thread.MIN_PRIORITY); jobs[i].start(); } try { for(;;) { jobs[nextThread].setPriority(Thread.NORM_PRIORITY); Thread.sleep(1000); jobs[nextThread].setPriority(Thread.MIN_PRIORITY); nextThread = (nextThread + 1) % jobs.length; } } catch(InterruptedException e) { System.out.println(e); } }