Threads in Java James Brucker.

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

Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
50.003: Elements of Software Construction Week 5 Basics of Threads.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Threads some important concepts Simon Lynch
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
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.
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.
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
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
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.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
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.
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.
Java Thread Programming
Threads.
A brief intro to: Parallelism, Threads, and Concurrency
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Asynchronous Programming
Multi Threading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Threads and Multithreading
Threads Chate Patanothai.
Threads II IS
Android Programming Lecture 8
Condition Variables and Producer/Consumer
Threads and Multithreading
Condition Variables and Producer/Consumer
Threads and Multithreading
Multithreading 2 Lec 24.
Multithreading.
Multithreaded Programming
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
Threads and Multithreading
Multithreading in java.
Threads and Multithreading
Threads and Multithreading
Using threads for long running tasks.
Representation and Management of Data on the Internet
Threads.
CMSC 202 Threads.
some important concepts
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threads in Java James Brucker

A Thread is a single flow of control statement1 Each statement must finish before next one is executed. This is the way our console-based programs run. statement2 read something statement4

A Program can create multiple threads Threads execute independently. They are not synchronized. thread1.start() thread2.start() statement1 statement1b statement2 statement2b read something statement3b statement4

Why Use Threads? Perform tasks in parallel connection to database I/O operations long-running computations (can be in parallel) Prevent user interface from "freezing" UI remains responsive while work is being done. Android requires responsive UI... or it will kill you! AsyncTask class for background tasks.

Basic way to create a Thread Use the Thread class. There are 2 ways: 1. extend Thread class and override run() public class PrompterThread extends Thread { private String message; public Prompter(String message) { this.message = message; } /** the work we want to perform */ public void run() { System.out.println( message );

Basic way to create a Thread 2. Define class that implements Runnable. Put an instance of this Runnable in a Thread. public class Prompter implements Runnable { private String message; public Prompter(String message) { this.message = message; } /** the work we want to perform */ public void run() { System.out.println( message ); Thread promptThread = new Thread( new Prompter() ); promptThread.start();

Running a Thread Call the start( ) method of Thread. It runs in a separate thread and returns control immediately // Method 1. use a subclass of Thread Thread thread1 = new PrompterThread("Hello"); thread1.start( ); // Method 2. use a Runnable and wrap in a Thread Runnable prompter = new Prompter("Hello"); Thread thread2 = new Thread( prompter ); thread2.start();

Sequence Diagram

Demo 1. Hello and Goodbye threads. Say "Hello", "and", "Goodbye" in separate threads, wait 500ms, repeat. ... the messages are not always synchronized. Note: if "main" method quits without waiting for threads or killing them, they will keep running, even if console window is closed.

Waiting for threads to finish After Thread.start() control returns immediately. To wait for another thread to finish, use join. public static void main(String[] args) { Thread thread1 = new Thread( new Greeter("Hello") ); thread1.start(); try { thread1.join(); } catch (InterruptedException ie) { // something interrupted the thread // while we were waiting }

InterruptedException A Thread can be interrupted by calling thread.interrupt(). If the thread is busy, waiting, or sleeping then an InterruptedException is thrown in the thread. try { Thread.sleep( 2000 ); // time in millisec } catch (InterruptedException ie) { // something invoked interrupt( ) while // we were sleeping! }

Checking for Interruptions If your thread might be interrupted, you can check for it using Thread.interrupted( ) class Downloader implements Runnable { public void run( ) { // read a big file into buffer while((size=in.read(buffer)) > 0) { // check for interruption if ( Thread.interrupted() ) { // close streams and return in.close(); return; } // otherwise, process the buffer

Thread class Thread currentThread( ): Thread interrupt( ): void interrupted(): boolean getName( ): String sleep(millisec: long): void many more methods

Interrupt Demo In the "Hello and Goodbye" demo modify main method: when user presses ENTER, interrupt the greeter threads. Use: thread.interrupt( ) - interrupt thread

Impatient Demo Write an impatient greeter: Prompt user his name and print "Hello, username." but don't wait more than 5 seconds for a reply. Use a separate thread to get the user's name. thread.join(timeout) - wait for thread at most timeout millisecs thread.interrupt( ) - interrupt thread thread.destroy( ) - doesn't

Demo 1. "main" waits for threads to finish. 2. In Hello / Goodbye, give each thread a name. new Thread( String name ) new Thread( Runnable task, String name ) 2. In each thread, get its name and print it. Use Thread.currentThread() and thread.getName().

Thread Managers and Thread Pools For most uses, you should use one of these: Timer - run a task at specified time or frequency ExecutorService - manage one or more threads (called a thread pool). - reuse threads (efficient memory use). - can also run Callable tasks (return a value in future) - shutdown or cancel tasks

Demo 1. Rewrite ImpatientGreeter to use ExecutorService: ExecutorService executor = Executors.newSingleThreadExecutor( ); executor.submit( runnable ); wait for thread: executor.awaitTermination(5, TimeUnit.SECONDS) kill threads: executor.shutdownNow( )

Demo 2. Rewrite Hello / Goodbye to use Timer. timer.schedule( TimerTask, long delay, long period ) timer.scheduleAtFixedRate( TimerTask, delay, period )

Defines several kinds of thread pools. ExecutorService Defines several kinds of thread pools. Example: You want to run some tasks using threads, but use at most 4 theads at one time (to avoid task switching). final int MAX_THREADS = 4; ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS); // add tasks to queue for execution executor.execute(runnable1); executor.execute(runnable2); . . . executor.execute(runnable10);

Protected Blocks of Code When multiple threads access the same object, or same memory, what can go wrong? What if two threads try to change some memory at the same time?

Thread-safe classes StringBuffer - thread safe (OK for 2 threads to share) StringBuilder - not thread sale AtomicInteger, AtomicLong, etc. - thread safe Integer, Long, etc.

"synchronized" methods If a method is "synchronized" then one one thread can execute it at a time. Other threads must wait. public synchronized List withdraw(double amount) { // only one thread can execute this method // at any time. Other threads must wait. Problem: synchronized methods are slooooow.

References The Java Tutorial: https://docs.oracle.com/javase/tutorial Threads & Executors Tutorial (Winterbe blog) http://winterbe.com/posts/2015/04/07/java8- concurrency-tutorial-thread-executor-examples/ This is a really good blog for Java! Have a look. Concurrency (general) https://docs.oracle.com/javase/tutorial/essential/concurr ency/index.html