Jan. 20041 Java Threads Yangjun Chen Dept. Business Computing University of Winnipeg.

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

Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
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-
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Lecture 4 : JAVA Thread Programming
Java Programming: Advanced Topics
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to 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.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
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.
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.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Threads.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and 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 Thread and Memory Model
Threading and Concurrency COM379T John Murray –
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.
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.
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.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Lecture10 Multithreading.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Distributed and Parallel Processing George Wells.
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
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
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:

Jan Java Threads Yangjun Chen Dept. Business Computing University of Winnipeg

Jan Outline: Multithreading Threads and Multithreading Thread Class Static Methods Instance Methods Creating a Thread -extending the Thread class -implementing the Runnable interface Synchronization in Threads Thread Communication Deadlock

Jan Threads A thread is a single stream of execution within a process. A process is a program executing in its own address space. You have been using threads all along. The main control or execution of all the programs up until now were controlled by a single thread. What we want to look at is mutlithreading or having multiple threads executing within the same program.

Jan Threads You might be more familiar with the term multitasking. Multitasking: -having more than one program working at what seems to be at the same time. -The OS assigns the CPU to the different programs in a manner to give the impression of concurrency. -There are two types of multitasking - preemptive and cooperative multitasking. Multithreading: -extends the idea of multitasking by allowing individual programs to have what appears to be multiple tasks. -Each task within the program is called a thread.

Jan Threads Java has multithreading built into it. Java provides a Thread class for handling threads. There are two ways to Thread objects -creating objects from subclasses of the Java Thread class -implementing the Runnable interface for an object Thread ThreadSubclass class ThreadX extends Thread { public void run() { //logic for the thread }} ThreadX tx = new ThreadX(); tx.start();

Jan Runnable SomeSubclass class RunnableY implements Runnable { public void run() { //logic for the thread }} RunnableY ry = new RunnableY(); Thread ty = new Thread(ry); ty.start(); Threads implements

Jan Thread Class The Thread class is part of the java.lang package. Using an object of this class, the corresponding thread can be stopped, paused, and resumed. There are many constructors and methods for this class, we will look at a few of them: -Thread( ) - no argument -Thread( String n) - creates a new Thread with the name n. -Thread( Runnable target) - creates a new Thread object. -Thread( Threadgroup group, Runnable target) This creates a new Thread object in the specified Threadgroup.

Jan Methods in Thread Class static methods: activeCount();currentThread();sleep();yield(); instance methods: getPriority();setPriority();start();stop();run();isAtive();suspend();resume();join();

Jan Static Methods of Thread Class static int activeCount() - returns the number of currently active threads. For example: -num_ threads = Thread. activeCount(); static Thread currentThread() - returns the object corresponding to the currently executing thread (self reference) -Thread myself = Thread. currentThread(); static void sleep( long millis) throws InterruptedException - this causes the current thread to sleep for the specified amount of time. Other threads will run at this time.

Jan Static Methods of Thread Class You can also specify the number of nanoseconds as well -static void sleep(long millis, int nano); static void yield() - causes the thread to yield the processor to any other waiting threads - Java does not guarantee preemption, you should use this to ensure fairness.

Jan Instance Methods of Thread Class These methods control the thread represented by a Thread object int getPriority() - returns the threads priority - a value between Thread. MIN_ PRIORITY and Thread. MAX_ PRIORITY void setPriority( int newpri) - this sets the threads priority - high priority threads will preempt lower ones when they become ready to run. Thread myself = Thread. currentThread(); myself. setPriority( Thread. MAX_ PRIORITY);

Jan Instance Methods of Thread Class A ThreadGroup may restrict the maximum priority of all its member threads - therefore the setPriority method may not succeed. void start() throws IllegalThreadStateException - actually starts the thread - the thread starts and enters the run() method void stop() throws SecurityException - stops the thread void run() - this method is called when the thread is started. This is what the thread will execute while it is alive. boolean isAlive() - returns a value indicating whether the thread is currently alive - i.e. Started more recently and has not yet been died.

Jan Instance Methods of Thread Class void suspend() - suspends the threads execution void resume() - resumes the execution of the thread void join() – causes the caller to wait until the thread dies

Jan Creating Threads Creating a thread by subclassing the Thread class This method will allow only five thread to be started in an object. public class SimpleThread extends Thread { private int countDown = 3; private static int threadCount = 0; private int threadNumber = ++threadCount; public SimpleThread( ) { System.out.println(“Making ” + threadNumber++); }

Jan Creating Threads public void run( ) { while(true) { System.out.println(“Thread ” + threadNumber + “(“ + countDown + “)”); if (--countDown == 0) return; } public static void main(String[] args) { for (int i = 0; i < 5; i++) new SimpleThread().start(); System.out.println(“All Threads Started”); }

Jan Creating Threads One possible output of ‘SimpleThread’: Making 1All Threads Started Making 2Thread 2(3) Making 3Thread 2(2) Making 4Thread 6(3) Making 5Thread 3(2) Thread 3(3)Thread 2(1) Thread 4(3)Thread 6(2) Thread 4(2)Thread 6(1) Thread 4(1)Thread 3(1) Thread 5(3) Thread 5(2) Thread 5(1)

Jan Creating Threads One possible output of ‘SimpleThread’: T main T2T2T2T2 T3T3T3T3 T4T4T4T4 T5T5T5T5 T6T6T6T6 Making 1 Making 2 Making 3 Making 4 Making 5 3(3)3(2) 3(1)4(3)4(2) 4(1)6(3)6(2) 6(1)5(3)5(2) 5(1)2(3)2(2) 2(1) All Thread started

Jan Synchronization in Threads Synchronization is a mechanism to control the the execution of different threads so that: -when multiple threads access a shared variable, proper execution can be assured. Java has the synchronized keyword - this can be used to identify a segment of code or method that should be accessible to just a single thread at a time. Before entering a synchronization region, a thread should obtain the semaphore associated with that region - it is already taken, then the thread blocks (waits) until the semaphore is released.

Jan Synchronization in Threads class Account { private int balance = 0; synchronized void deposit(int amount)} balance += amount; }} class Customer extends Thread { Account account; Customer(Account account) { this.account = account; } public void run() { try { for (int i = 0; i < 10000; i++) {account.deposit(10);} }

Jan Synchronization in Threads catch (Exception e) { e.printStackTrace(); } }} public class BankDemo { private final static int NUMCUSTOMER = 10; public static void main(String args[]) { //Create account Account account = new Account(); //Create and start customer threads Customer customer[] = new Customer[NUMCUSTOMER]; for (int i = 0; i < NUMCUSTOMER; i++) { customer[i] = new Customer(account); customer[i].start(); }

Jan Synchronization in Threads //Wait for customer threads to complete for (int i = 0; i < NUMCUSTOMER; i++) { try { customer[i].join(); } catch (InterruptedException e) { e.printStackTrace(); } //Display account balance System.out.println(account.getBalance()); }

Jan Synchronization in Threads In Java, any object with one or more synchronized methods is a monitor. When threads call a synchronized method, only one thread is let in at a time, the others wait in a queue. In producer- consumer type applications, consumer threads might find that there is not enough elements to consume It is the job of the monitor to ensure that the threads that are waiting for the producer are notified once the elements are produced.

Jan Thread Communication A thread can temporarily release a lock so other threads can have an opportunity to execute a synchronized method. It is because the Object class defined three methods that allow threads to communicate with each other. -void wait() - causes the thread to wait until notified - this method can only be called within a synchronized method. - void wait(long msec) throws InterruptedException - void wait(long msec, int nsec) throws InterruptedException -void notify() - notifies a randomly selected thread waiting for a lock on this object - can only be called within a synchronized method. -void notifyall() - notifies all threads waiting for a lock on this object - can only be called within a synchronized method.

Jan Thread Communication class Producer extends Thread { Queue queue; Producer (Queue queue) { this.queue = queue; } public void run { int i = 0; while(true) { queue.add(i++); }

Jan Thread Communication class Comsumer extends Thread { String str; Queue queue; Consumer (String str, Queue queue) { this.str = str; this.queue = queue; } public void run { while(true) { System.out.println(str + “: ” + queue.remove();); }

Jan Thread Communication class queue { private final static int SIZE = 10; int array[] = new int[SIZE]; int r = 0; int w = 0; int count = 0; synchronized void add(int i) { //wait while the queue is full while (count == SIZE) { try { wait() } catch (InterruptedException ie) { ie.printStackTrace(); System.exit(0); }}

Jan Thread Communication //Add data to array and adjust write pointer array[w++] = i; if (w >= SIZE) w = 0; //Increment count ++count; //Notify waiting threads notifyAll(); } synchronized int remove() { //wait while the queue is empty while (count == 0) { try { wait();} catch (InterruptedException ie) { ie.printStackTrace(); System.exit(0);}}

Jan Thread Communication //read data from array and adjust read pointer int element = array[r++]; if (r >= SIZE) r = 0; //Decrement count --count; //Notify waiting threads notifyAll(); return element; }} public ProducerConsumer { public static void main(String args[]) { Queue queue = new Queue(); new Producer(queue).start(); new Consumer(“ConsumerA”, queue).start(); new Consumer(“ConsumerB”, queue).start(); new Consumer(“ConsumerC”, queue).start();}}

Jan Thread Properties runnable blocked new dead start stop I/Ocomplete block on I/O notify wait resume suspend done sleep

Jan Deadlock Deadlock is an error that can be encountered in multithreads. It occurs when two or more threads wait indefinitely for each other to relinquish locks. -Assume that thread-1 holds a lock on object-1 and waits for a lock on object-2. Thread-2 holds a lock on object-2 and waits for a lock on object- 2. -Neither of these threads may proceed. Each waits forever for the other to relinquish the lock it needs.

Jan Deadlock T1:Aa1(); a2(); b B a b1(); b2(); T2:

Jan Deadlock class A { B b; synchronized void a1() { System.out.println(“Starting a1”); b.b2(); } synchronized void a2() { System.out.println(“Starting a2”); }

Jan Deadlock class B { A a; synchronized void b1() { System.out.println(“Starting b1”); a.a2(); } synchronized void b2() { System.out.println(“Starting b2”); }

Jan Deadlock class Thread1 extends Thread { A a; Thread1(A a) { this.a = a; } public void run() { for (int i = 0; i < ; i++) a.a1(); }

Jan Deadlock class Thread2 extends Thread { B b; Thread2(B b) { this.b = b; } public void run() { for (int i = 0; i < ; i++) b.b1(); }

Jan Deadlock public class DeadlockDemo { public static void main(String args[]) { //Create objects A a = new A(); B b = new B(); a.b = b; b.a = a; //Create threads Thread1 t1 = new Thread1(a); Thread2 t2 = new Thread2(b); t1.start(); t2.start();} //wait for threads to complete try {t1.join(); t2.join();} catch (Exception e) { e.printStackTrace(); } System.out.println(“Done!”); }}

Jan Deadlock The following is sample output from this application: Starting a1 Starting b2 Starting a1 Starting b2 Starting a1 Starting b2 Starting a1 Starting b2 Starting a1 Starting b1

Jan public class GenericServer extends Thread { protected ServerSocket s; protected HashTable routes; public GenericServer( int port) throws IOException { super(“GenericServer”); s = new ServerSocket( port); routes = new HashTable(); public void run() { try { while( true) { Socket s2 = s. accept(); // other code } catch( IOException e) { e. printStackTrace(); }

Jan public static void main( String args[]) throws IOException { if (args. length!= 1) throw new IOException(“ Syntax: GenericServer ”); GenericServer server = new GenericServer (Integer. parseInt( args[ 0])); server. start(); } // end of main } // end of class