1 Java Threads Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Concurrent Programming Abstraction & Java Threads
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Threads CS Introduction to Operating Systems.
Tutorial 2 Adventures in Threading presented by: Antonio Maiorano Paul Di Marco.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
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.
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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
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.
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
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
Java Thread and Memory Model
Threading and Concurrency COM379T John Murray –
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Java 3: Odds & Ends Advanced Programming Techniques.
Multi-Threading in Java
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
THREAD MODEL.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
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 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.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multithreading Lec 23.
Multithreading.
Multithreaded Programming in Java
Multithreading in Java
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreaded Programming in Java
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Java Thread.
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Lecture 19 Threads CSE /6/2019.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 Java Threads Instructor: Mainak Chaudhuri

2 Java threads Two ways to create a thread –Extend the Thread class and override the public run method –Implement a runnable interface with a public run method class MyThread extends Thread { private int tid; private String name; private long startTime; public MyThread (String name, int tid) { super (name); this.tid = tid; this.name = new String(name); startTime = System.currentTimeMillis(); } // next slide

3 Java threads public void run () { // override System.out.println (“ Hi! This is ” + getName() + “.”); } public long age () { return (System.currentTimeMillis () – startTime); } } // end class class MyFirstThreadDemo { public static void main (String arg[]) { Integer n = new Integer (arg[0].trim()); int numThreads = n.intValue(); // next slide

4 Java threads int i; MyThread t[] = new MyThread[numThreads]; for (i=0; i<numThreads; i++) { t[i] = new MyThread (“Thread ”+i, i); t[i].start(); } } // end main } // end class

5 Java threads via interface class MyThread implements Runnable { private int tid; private String name; private long startTime; public MyThread (String name, int tid) { this.tid = tid; this.name = new String(name); startTime = System.currentTimeMillis(); } public void run () { System.out.println (“ Hi! This is ” + Thread.currentThread().getName() + “.”); } //next slide

6 Java threads via interface public long age () { return (System.currentTimeMillis () – startTime); } } // end class class MyFirstThreadDemo { public static void main (String arg[]) { Integer n = new Integer (arg[0].trim()); int numThreads = n.intValue(); int i; // next slide

7 Java threads via interface MyThread mt[] = new MyThread[numThreads]; Thread t[] = new Thread[numThreads]; for (i=0; i<numThreads; i++) { mt[i] = new MyThread (“Thread ”+i, i); t[i] = new Thread (mt[i], “Thread ”+i); t[i].start(); } } // end main } // end class

8 Announcements Final exam on 19 th November 1215 to 1515 Seating arrangement: Y4, Y5, Y7001 to Y7085L1 OROS Y7092 to Y7233L2 ERES Y7234 to Y7388L16 OROS Y7391 to Y7518L17 ERES Please take seats 15 minutes before the scheduled time

9 Java thread states Java threads have typically seven states –New: just after a thread is created –Runnable: after the start() method is invoked. The thread is placed in the runnable set or the ready queue. When scheduled, the run() method will be executed. –Running: executing the code in the run() method. May call its yield() method to put itself back in the runnable set. –Suspended: after the suspend() method is called. May be called by itself or by some other thread. It is placed back on runnable set only if some other thread calls its resume() method.

10 Java thread states Seven states (continued) –Blocked: after the sleep() method or the wait() method or the join() method is called to join with a thread (at a barrier) that is yet to arrive at the join point or when it does blocking I/O (e.g., reading from keyboard). The thread transitions back to runnable state when the blocking call is over. –Suspended-blocked: If a blocked thread is suspended by some other thread. It returns back to suspended or blocked state depending on whether the blocking finishes before a call to resume() or not. –Dead: On completion of the run() method or a call to stop() method.

11 Java thread scheduling Every Java thread gets a priority –Inherits the same priority from the creating thread. In our example, all threads have same priority as the “main thread”. –Priorities range from MIN_PRIORITY to MAX_PRIORITY defined in Thread class. The default priority is NORM_PRIORITY. –Use setPriority and getPriority methods to change and query a thread’s priority. –Among all the threads in the runnable set, the highest priority thread is allowed to run until it blocks, yields, gets suspended, or a new thread of higher priority enters the runnable set. In the last case the running thread goes back to runnable set

12 Java thread scheduling Some implementations carry out a round- robin scheduling (e.g., JDK 1.1 for Windows 95/NT) –Every thread gets a time slice to execute –Once the time slice of the thread expires, it is put back in the runnable set and the next thread is given a chance (higher priority threads are considered first) –If a thread blocks, yields, or gets suspended before the time slice expires, the next thread is scheduled for execution

13 More example class MyThread extends Thread { private int tid; private String name; private long startTime; private long screamingInterval; public MyThread (String name, int tid, long screamingInterval) { super (name); this.tid = tid; this.name = new String(name); startTime = System.currentTimeMillis(); this.screamingInterval = screamingInterval; } // next slide

14 More example public void run () { // override long count = 0; while (true) { if (count % screamingInterval == 0) { System.out.println (“ Hi! This is ” + getName() + “.”); } count++; } public long age () { return (System.currentTimeMillis () – startTime); } } // end class

15 More example class MyThreadDemo { public static void main (String arg[]) { Integer n = new Integer (arg[0].trim()); int numThreads = n.intValue(); int i; MyThread t[] = new MyThread[numThreads]; for (i=0; i<numThreads; i++) { t[i] = new MyThread (“Thread ”+i, i, (i+1)* ); //t[i].setPriority (t[i].getPriority()+i); t[i].start(); } } // end main } // end class

16 Typical output snapshot Hi! This is Thread 0. Hi! This is Thread 1. Hi! This is Thread 0. Hi! This is Thread 1. Hi! This is Thread 0. Hi! This is Thread 1. Hi! This is Thread 0. Hi! This is Thread 1. Hi! This is Thread 0.

17 Physical resources Parallelism ultimately depends on availability of multiple physical CPUs –You get true concurrency with “multiprocessors” –With single CPU, you get time-shared concurrency (at any point in time only one thread can run) –Today small-scale multiprocessors are commodity –Chip-multiprocessors or so called multi-core processors come with two, four, or eight processors on a single chip (soon 16 processors)

18 Data race How to share a variable? –Create a single class containing the variables that you want to share –Create one instance of this object and attach multiple threads to it –Let us see what happens if we try to update a shared variable concurrently in multiple threads –This is known as a data race as multiple threads may have a race trying to update the same data

19 Data race class MyThread implements Runnable { private long sum; public MyThread () { sum=0; } public void run () { int i; for (i=0; i<100000; i++) { sum++; } System.out.println(" : Sum=" + sum); } // next slide

20 Data race public long GetSum () { return sum; } } // end class class MyDataRaceDemo { public static void main (String arg[]) throws java.lang.InterruptedException { Integer n = new Integer (arg[0].trim()); int numThreads = n.intValue(); int i; // next slide

21 Data race MyThread mt = new MyThread(); Thread t[] = new Thread[numThreads]; for (i=0; i<numThreads; i++) { t[i] = new Thread (mt, "Thread "+i); t[i].start(); } for (i=0; i<numThreads; i++) { t[i].join(); } System.out.println("From main: Sum=" + mt.GetSum()); } // end main } // end class

22 Data race If you run this program multiple times, you will get different outputs in different runs –A typical symptom of data race –The output depends on how the threads get interleaved when they run –A typical output: : Sum= : Sum= From main: Sum=128853

23 More data race example Let us try to write the program for summing an array class MyThread implements Runnable { private int array[]; private long sum; private int numThreads; private int tid; public MyThread (long size, int numThreads) { int i; array = new int[size]; for (i=0; i<size; i++) { array[i] = i; } sum = 0; tid = 0; this.numThreads = numThreads; } // next slide

24 More data race example public void run () { int i; int myid = tid++; // data race on tid for (i=myid*(array.length/numThreads); i<(myid+1)*(array.length/numThreads); i++) { sum += array[i]; // data race on sum } public long GetSum() { return sum; } } // end class

25 More data race example class MyArraySumDemo { public static void main (String arg[]) throws java.lang.InterruptedException { Integer n = new Integer (arg[0].trim()); long size = n.intValue(); n = new Integer (arg[1].trim()); int numThreads = n.intValue(); int i; MyThread mt = new MyThread(size, numThreads); Thread t[] = new Thread[numThreads]; // next slide

26 More data race example for (i=0; i<numThreads; i++) { t[i] = new Thread (mt, "Thread "+i); t[i].start(); } for (i=0; i<numThreads; i++) { t[i].join(); } System.out.println("From main: Sum=" + mt.GetSum() + “[Expected=” + (size*(size-1))/2 + “]”); } // end main } // end class

27 More data race example One typical output for size= and numThreads=2 From main: Sum= [Expected= ] Replacing sum by local sum and accumulating at the end reduces the chance of data race –Reduces the number of executed critical sections: more parallelism –But the program is still buggy and may produce wrong results once in a while

28 More data race example class MyThread implements Runnable { private int array[]; private long sum; private int numThreads; private int tid; public MyThread (long size, int numThreads) { int i; array = new int[size]; for (i=0; i<size; i++) { array[i] = i; } sum = 0; this.numThreads = numThreads; } // next slide

29 More data race example public void run () { int i; long localSum = 0; // private variable int myid = tid++; // data race on tid for (i=myid*(array.length/numThreads); i<(myid+1)*(array.length/numThreads); i++) { localSum += array[i]; // no data race } sum += localSum; // data race on sum } public long GetSum() { return sum; } } // end class

30 More data race example class MyArraySumDemo { public static void main (String arg[]) throws java.lang.InterruptedException { Integer n = new Integer (arg[0].trim()); long size = n.intValue(); n = new Integer (arg[1].trim()); int numThreads = n.intValue(); int i; MyThread mt = new MyThread(size, numThreads); Thread t[] = new Thread[numThreads]; // next slide

31 More data race example for (i=0; i<numThreads; i++) { t[i] = new Thread (mt, "Thread "+i); t[i].start(); } for (i=0; i<numThreads; i++) { t[i].join(); } System.out.println("From main: Sum=" + mt.GetSum() + “[Expected=” + (size*(size-1))/2 + “]”); } // end main } // end class

32 Fixing data races Every Java object has an in-built lock –We will make use of this to protect the critical sections –For every shared variable that is involved in a data race, we need to create a lock –It is possible to have a single lock to resolve multiple races, but that will limit concurrency (why?) –General solution: create a dummy object and use its lock to resolve a race –Java allows the programmer to mark critical sections with the keyword synchronized

33 Fixing array sum class MyThread implements Runnable { private int array[]; private long sum; private int numThreads; private int tid; private Object tidLock; // dummy object private Object sumLock; // dummy object public MyThread (long size, int numThreads, Object tidLock, Object sumLock) { int i; array = new int[size]; for (i=0; i<size; i++) { array[i] = i; } // next slide

34 Fixing array sum sum = 0; this.numThreads = numThreads; this.tidLock = tidLock; this.sumLock = sumLock; } public void run () { int i; int myid; synchronized (tidLock) { myid = tid++; } // next slide

35 Fixing array sum for (i=myid*(array.length/numThreads); i<(myid+1)*(array.length/numThreads); i++) { synchronized (sumLock) { sum += array[i]; } public long GetSum() { return sum; } } // end class

36 Fixing array sum class MyArraySumDemo { public static void main (String arg[]) throws java.lang.InterruptedException { Integer n = new Integer (arg[0].trim()); long size = n.intValue(); n = new Integer (arg[1].trim()); int numThreads = n.intValue(); int i; Object tidLock = new Object(); Object sumLock = new Object(); MyThread mt = new MyThread(size, numThreads, tidLock, sumLock); Thread t[] = new Thread[numThreads]; // next slide

37 Fixing array sum for (i=0; i<numThreads; i++) { t[i] = new Thread (mt, "Thread "+i); t[i].start(); } for (i=0; i<numThreads; i++) { t[i].join(); } System.out.println("From main: Sum=" + mt.GetSum() + "[Expected=" + (size*(size-1))/2 + "]"); } // end main } // end class

38 Announcements No class tomorrow All the best for the final exam. Bye!