Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Concurrency 101 Shared state. Part 1: General Concepts 2.
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.
Practice Session 7 Synchronization Liveness Guarded Methods Model
System Programming Practical Session 5 Liveness, Guarded Methods, and Thread Timing.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
11-Jun-15 Producer-Consumer An example of using Threads.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Multithreading.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads some important concepts Simon Lynch
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
10/17/2015Vimal1 Threads By 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
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.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Java Threads 1 1 Threading and Concurrent Programming in Java Queues D.W. Denbo.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
Threading Eriq Muhammad Adams J
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Multithreading in JAVA
1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science.
Threads. Story so far  Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked.
הקדמה תזמון משימות משתנים אטומיים מנעולים Executor ThreadPools מבני נתונים מסונכרנים הטמעה תכנות מתקדם - תרגול 6 נושאים מתקדמים ב Threads אליהו.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
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.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
System Programming Practical Session 4: Concurrency / Safety.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
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.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
…in java DThread Pools x. Thread Pools: Why? Source code updates – copy/paste of run/runnable method for each thread is exhausting There is overhead to.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Advanced Programming Concurrency and Threads Advanced Programming. All slides copyright: Chetan Arora.
Principles of Software Development
Multithreaded applets
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
Multi Threading.
Thread Pools (Worker Queues) cs
Practice Session 8 Lockfree LinkedList Blocking queues
Lecture 24 Concurrency 2 (D&D 23) Date.
System Programming Practical Session 7
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Threads in Java James Brucker.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
some important concepts
More concurrency issues
Presentation transcript:

Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Introduction By now, you know how to program threads But in a very basic level that matches low- level implementations For higher-level code, we need advanced tools  Tools that will hide the threads logic from us  Makes it easier for us to control threads  Mostly are from java.util.concurrent  introduced in java 1.5

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Scheduling Tasks We want an ordered “ping- pong” sequence Half a second apart Does this code meet the demands? public class ThreadTest { private static class Ping implements Runnable{ public void run(){while(true)System.out.println("ping");} } private static class Pong implements Runnable{ public void run(){while(true)System.out.println("pong");} } public static void main(String[] args) { Ping ping=new Ping(); Pong pong=new Pong(); Thread t=new Thread(ping,"thread 1"); Thread t1=new Thread(pong,"thread 2"); t.start(); t1.start(); }

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Scheduling Tasks We already saw a solution using wait() Here is a solution using sleep() public class ThreadTest { private static class Ping implements Runnable{ public void run(){ while(true){ System.out.println("ping"); try {Thread.sleep(1000);} catch (InterruptedException e) {} } private static class Pong implements Runnable{ … public static void main(String[] args) throws InterruptedException { Ping ping=new Ping(); Pong pong=new Pong(); Thread t=new Thread(ping,"thread 1"); Thread t1=new Thread(pong,"thread 2"); t.start(); long time2,time=System.currentTimeMillis(); while((time2=System.currentTimeMillis())-time<500); t1.start(); }} public static void main(String[] args) throws InterruptedException { Ping ping=new Ping(); Pong pong=new Pong(); Thread t=new Thread(ping,"thread 1"); Thread t1=new Thread(pong,"thread 2"); t.start(); long time2,time=System.currentTimeMillis(); while((time2=System.currentTimeMillis())-time<500); t1.start(); }}

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Scheduling Tasks A far more simple way – use a timer! import java.util.Timer; import java.util.TimerTask; public class ThreadTest { private static class Ping extends TimerTask{ public void run(){System.out.println("ping");} } private static class Pong extends TimerTask{ public void run(){System.out.println("pong");} } public static void main(String[] args){ Ping ping=new Ping(); Pong pong=new Pong(); Timer t=new Timer(); t.scheduleAtFixedRate(ping, 0, 1000); t.scheduleAtFixedRate(pong, 500, 1000); } import java.util.Timer; import java.util.TimerTask; public class ThreadTest { private static class Ping extends TimerTask{ public void run(){System.out.println("ping");} } private static class Pong extends TimerTask{ public void run(){System.out.println("pong");} } public static void main(String[] args){ Ping ping=new Ping(); Pong pong=new Pong(); Timer t=new Timer(); t.scheduleAtFixedRate(ping, 0, 1000); t.scheduleAtFixedRate(pong, 500, 1000); } int i; while((i=System.in.read())!=13); ping.cancel(); // canceled task pong.cancel(); // t continues… t.cancel(); // t is cancled int i; while((i=System.in.read())!=13); ping.cancel(); // canceled task pong.cancel(); // t continues… t.cancel(); // t is cancled Canceling tasks:

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Atomic objects Last time we synchronized the update() method, since count++ is an atomic action It took about 46 seconds to complete public class Count { private int count; public void setCount(int x){count=x;} public int getCount(){return count;} synchronized public synchronized void update(){count++;} } public class ThreadTest { private static class CountAdapter implements Runnable{ Count c; public CountAdapter(Count c){ this.c=c; } public void run(){ for(int i=0;i< ;i++) c.update(); } public static void main(String[] args) { Count c=new Count(); c.setCount(0); CountAdapter ca=new CountAdapter(c); Thread t=new Thread(ca); Thread t1=new Thread(ca); long time=System.currentTimeMillis(); t.start(); t1.start(); while(t.isAlive() || t1.isAlive()); System.out.println(c.getCount()); System.out.println((System.currentTimeMillis()-time)/1000); }}

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Atomic objects We use an AtomicInteger instead of int There is no synchronized Took only 6 seconds to complete (with the desired result) import java.util.concurrent.atomic.AtomicInteger; public class Count { private AtomicInteger count = new AtomicInteger(0); public void setCount(int x){count.set(x);} public int getCount(){return count.get();} public void update(){ count.incrementAndGet();// count++ } public class ThreadTest { private static class CountAdapter implements Runnable{ Count c; public CountAdapter(Count c){ this.c=c; } public void run(){ for(int i=0;i< ;i++) c.update(); } public static void main(String[] args) { Count c=new Count(); c.setCount(0); CountAdapter ca=new CountAdapter(c); Thread t=new Thread(ca); Thread t1=new Thread(ca); long time=System.currentTimeMillis(); t.start(); t1.start(); while(t.isAlive() || t1.isAlive()); System.out.println(c.getCount()); System.out.println((System.currentTimeMillis()-time)/1000); }}

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Deadlock Example public class Friend { private String name; public Friend(String name){this.name = name;} public String getName(){return this.name;} public synchronized void sayHA(Friend date) { System.out.format("%s says HA to %s%n",this.name, date.getName()); date.sayDA(this); } public synchronized void sayDA(Friend date) { System.out.format("%s says DA to %s%n",this.name,date.getName()); } public class DeadLock { private static class FriendRun implements Runnable{ private Friend me,date; public FriendRun(Friend m,Friend d){ me=m;date=d;} public void run(){ me.sayHA(date); } } public static void main(String[] args) { Friend alice = new Friend("Alice"); Friend bob = new Friend("Bob"); Thread t=new Thread(new FriendRun(alice,bob)); Thread t1=new Thread(new FriendRun(bob,alice)); t.start(); t1.start(); }} Alice says HA to Bob Bob says HA to Alice Since in dates the topic is usually about “HA and DA” an icebreaker protocol was devised: The first person says “ha” and the other should reply “da” This is an implementation of the protocol. Will it work?

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Deadlock Example public class Friend { private String name; public Friend(String name){this.name = name;} public String getName(){return this.name;} public synchronized void sayHA(Friend date) { System.out.format("%s says HA to %s%n",this.name, date.getName()); date.sayDA(this); } public synchronized void sayDA(Friend date) { System.out.format("%s says DA to %s%n",this.name,date.getName()); } public class DeadLock { private static class FriendRun implements Runnable{ private Friend me,date; public FriendRun(Friend m,Friend d){ me=m;date=d;} public void run(){ me.sayHA(date); } } public static void main(String[] args) { Friend alice = new Friend("Alice"); Friend bob = new Friend("Bob"); Thread t=new Thread(new FriendRun(alice,bob)); Thread t1=new Thread(new FriendRun(bob,alice)); t.start(); t1.start(); }} A B t1 FR AB t AB A.sayHA(B) A is locked by t Start() B.sayHA(A) B is locked by t1 Start() t: B.sayDA(A) Impossible, B is locked by t1 A.sayHa(B) will not finish, A remains locked! t1: A.sayDA(B) Impossible, A is locked by t B.sayHa(A) won’t finish, B remains locked! Neither side has finished saying HA tt1

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Solving deadlocks with locks import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Friend { private String name; protected Lock lock = new ReentrantLock(); public Friend(String name){this.name = name;} public String getName(){return this.name;} public boolean iCanLockBoth(Friend date) { Boolean myLock = false; Boolean yourLock = false; try { myLock=lock.tryLock(); yourLock=date.lock.tryLock(); } finally { if (!(myLock&&yourLock)) { if (myLock) lock.unlock(); if (yourLock) date.lock.unlock(); } return myLock&&yourLock; } iCanLockBoth will return true only if we managed to lock both locks (by the same thread). Only then, will the friend say “ha” and call the other to say “da”, otherwise it will say it couldn’t say ha… public void sayHA(Friend date) { if(iCanLockBoth(date)){ try{ System.out.format("%s says HA to %s%n",this.name,date.getName()); date.sayDA(this); } finally{ lock.unlock(); date.lock.unlock(); } } else System.out.format("%s started to say HA but realized %s already started",name,date.getName()); } Alice says HA to Bob Bob says DA to Alice Bob started to say HA but realized Alice already started

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Executor Interface So far we used threads with Runnable’s public void run() method There was a direct attachment between the task and the thread that runs it… Sometimes we want to decouple these two  Controlling the number of threads  Scheduling tasks  etc Therefore an Executor Interface was created

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Executor Interface It has one method:  public void execute(Runnable r) We can implement it as we wish class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); }

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Executor Interface We can also use a queue  Each execute puts the runnable at the end  Only the first n are polled out of the queue  They are ran via a thread  When a task has finished, another is polled This is called a Thread Pool It is commonly used to control the number of threads, clients etc… Strong and flexible thread pools are already implemented for us

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Thread Pools import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadTest3 { private static void delay(long ms){ try { Thread.sleep(ms);} catch (InterruptedException e) {} } private static class RunnableTask1 implements Runnable{ public void run(){ System.out.println("task1 started"); delay(10000); System.out.println("task1 finished"); } } … public static void main(String[] args) { Executors.newSingleThreadExecutor(); Executor executor = Executors.newSingleThreadExecutor(); executor.execute (new RunnableTask1 ()); executor.execute (new RunnableTask2 ()); executor.execute (new RunnableTask3 ()); ((ExecutorService) executor).shutdown(); } Executors class has a factory of thread pools, a SingleThreadExecutor allows only one thread to be ran at a time What would be the output? task1 started task1 finished task2 started task2 finished task3 started task3 finished

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Thread Pools import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadTest3 { private static void delay(long ms){ try { Thread.sleep(ms);} catch (InterruptedException e) {} } private static class RunnableTask1 implements Runnable{ public void run(){ System.out.println("task1 started"); delay(10000); System.out.println("task1 finished"); } } … public static void main(String[] args) { Executors. newFixedThreadPool (2); Executor executor = Executors. newFixedThreadPool (2); executor.execute (new RunnableTask1 ()); executor.execute (new RunnableTask2 ()); executor.execute (new RunnableTask3 ()); ((ExecutorService) executor).shutdown(); } How about now? task1 started task2 started task1 finished task2 finished task3 started task3 finished

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Thread Pools import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadTest3 { private static void delay(long ms){ try { Thread.sleep(ms);} catch (InterruptedException e) {} } private static class RunnableTask1 implements Runnable{ public void run(){ System.out.println("task1 started"); delay(10000); System.out.println("task1 finished"); } } … public static void main(String[] args) { Executors. newCachedThreadPool(); Executor executor = Executors. newCachedThreadPool(); executor.execute (new RunnableTask1 ()); executor.execute (new RunnableTask2 ()); executor.execute (new RunnableTask3 ()); ((ExecutorService) executor).shutdown(); } How about now? task1 started task2 started task3 started task1 finished task3 finished task2 finished

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Callable Runnabler’s run() method  Cannot return a value  Cannot throw an exeption A Callable Interface can Interface Callable V call() throws Exception;

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Future Executors can submit a Callable  Using the submit(Callable c) method But what should submit return??  c.call() return V, that could be anything… Submit return a special object called Future  Future submit(Callable c)  V should be the same V of the Callable

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Callable, Future import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ThreadTest3 { private static void delay(long ms){ try { Thread.sleep(ms);} catch (InterruptedException e) {} } private static class CallableTask implements Callable { Integer i; public CallableTask(int i){ this.i=new Integer(i); } public Integer call() throws Exception { System.out.printf("task%d started\n",i); delay(10000); return i; } public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newCachedThreadPool(); Future futures[]=new Future[3]; for(int i=0;i<3;i++) futures[i]=executor.submit (new CallableTask(i+1)); executor.shutdown(); for(int i=0;i<3;i++) System.out.printf("task%d finished\n",futures[i].get()); } task1 started task2 started task3 started task1 finished task2 finished task3 finished

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Thread Safe Containers Most of java.util containers are not thread safe  Because synchronize slows performance They could be wrapped with synchronized decorators  Only when we must, we’ll pay with performance private Map hm = Collections.synchronizedMap( new HashMap ());

Introduction Scheduling Tasks Atomic objects Deadlock avoidance Thread Pools Callable, Future Thread safe containers Thread Safe Containers java.util.concurrent introduced Thread Safe containers, that also provides good performance!  ArrayBlockingQueue ArrayBlockingQueue  ConcurrentHashMap ConcurrentHashMap  ConcurrentLinkedQueue ConcurrentLinkedQueue  etc…