Threads.

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

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng 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.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
8-Oct-15 Threads and Multithreading. 2 Thread s A Thread is a single flow of control When you step through a program, you are following a Thread A Thread.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
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)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
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.
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Concurrent Computing 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.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Java Thread Programming
Multithreaded applets
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Multi Threading.
Threads and Multithreading
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
Chapter 19 Java Never Ends
SE /11/2018 If you think the internet is not working in its current incarnation, you can’t change the system through think-pieces and F.C.C. regulations.
Clients and Servers 19-Nov-18.
Threads and Multithreading
Clients and Servers 1-Dec-18.
Multithreading.
Threads and Multithreading
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Threads and Multithreading
Threads and Multithreading
Java Threads (Outline)
Java Threads (Outline)
Threads and Multithreading
9. Threads SE2811 Software Component Design
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
Threads and Multithreading
Threads in Java James Brucker.
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Threads and Multithreading
9. Threads SE2811 Software Component Design
CMSC 202 Threads.
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
9. Threads SE2811 Software Component Design
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threads

Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches: Cooperative multiprocessing Preemptive multiprocessing Really having multiple processors

Multithreading Multithreading programs appear to do more than one thing at a time Same ideas as multiprocessing, but within a single program More efficient than multiprocessing Java tries to hide the underlying multiprocessing implementation

Why multithreading? Allows you to do more than one thing at once Play music on your computer’s CD player, Download several files in the background, while you are writing a letter Multithreading is essential for animation One thread does the animation Another thread responds to user inputs

Threads A Thread is a single flow of control When you step through a program, you are following a Thread Your previous programs all had one Thread A Thread is an Object you can create and control

Sleeping Every program uses at least one Thread Thread.sleep(int milliseconds); try { Thread.sleep(1000); } catch (InterruptedException e) { } sleep only works for the current Thread

States of a Thread A Thread can be in one of four states: Ready: all set to run Running: actually doing something Waiting, or blocked: needs something Dead: will never do anything again State names vary across textbooks You have some control, but the Java scheduler has more

State transitions ready waiting running dead start

Two ways of creating Threads You can extend the Thread class: class Animation extends Thread {…} Limiting, since you can only extend one class Or you can implement the Runnable interface: class Animation implements Runnable {…} requires public void run( ) I recommend the second for most programs Applets must use the second method (since they must extend Applet)

Extending Thread class Animation extends Thread { public void run( ) { code for this thread } Anything else you want in this class } The run() method overrides the one inherited from Thread Animation anim = new Animation( ); A newly created Thread is in the Ready state To start the anim Thread running, call anim.start( ); start( ) is a request to the scheduler to run the Thread --it may not happen right away The Thread should eventually enter the Running state

Implementing Runnable class Animation implements Runnable { public void run( ) { code for this thread } Anything else you want in this class } The Runnable interface requires run( ) This is the “main” method of your new Thread Animation anim = new Animation( ); Thread myThread = new Thread(anim); To start the Thread running, call myThread.start( ); You do not write the start() method—it’s provided by Java As always, start( ) is a request to the scheduler to run the Thread--it may not happen right away

Starting a Thread Every Thread has a start( ) method Do not write or override start( ) You call start( ) to request a Thread to run The scheduler then (eventually) calls run( ) You must supply public void run( ) This is where you put the code that the Thread is going to run

Extending Thread: summary class Animation extends Thread { public void run( ) { while (okToRun) { ... } } Animation anim = new Animation( ); anim.start( );

Implementing Runnable: summary class Animation extends Applet implements Runnable { public void run( ) { while (okToRun) { ... } } Animation anim = new Animation( ); Thread myThread = new Thread(anim); myThread.start( );

Things a Thread can do Commonly used: Rarely used: Thread.sleep(milliseconds) Rarely used: yield( ) Thread me = currentThread( ); int myPriority = me.getPriority( ); me.setPriority(NORM_PRIORITY); if (otherThread.isAlive( )) { … } join(otherThread);

Animation requires two Threads Suppose you set up Buttons and attach Listeners to those buttons... …then your code goes into a loop doing the animation… …who’s listening? Not this code; it’s busy doing the animation Whenever you create a GUI, Java automatically creates a second thread to handle all the routine stuff (Buttons, text fields, etc.) If you do drawing (lines, circles, text not in a text field, etc.), it is your responsibility to tell Java when anything is changed and needs to be redrawn

How to do animations Create your buttons and attach listeners in your first (original) Thread Java automatically creates another Thread to handle your listeners You should explicitly create another Thread to run the animation start() the animation The original Threads are free to listen to the buttons

Things a Thread should NOT do The Thread controls its own destiny Deprecated methods myThread.stop( ) myThread.suspend( ) myThread.resume( ) Outside control turned out to be a Bad Idea Don’t ever use these methods!

How to control another Thread Don’t use the deprecated methods! Instead, put a request where the other Thread can find it boolean okToRun = true; animation.start( ); public void run( ) { while (controller.okToRun) {…}

A problem int k = 0; Thread #1: k = k + 1; Thread #2: System.out.print(k); What gets printed as the value of k? This is a trivial example of what is, in general, a very difficult problem

Tools for a solution You can synchronize an object: synchronized (obj) { code that uses/modifies obj } No other code can use or modify this object at the same time You can synchronize a method: synchronized void addOne(arg1, arg2, ...) { code } Only one synchronized method in a class can be used at a time (non-synchronized methods can still be used) Synchronization is a tool, not a solution—multithreading is in general a very hard problem

The End