Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Multithreading The objectives of this chapter are:
Java Threads Part I. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications in.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread.
Concurrency…leading up to writing a web crawler. Web crawlers.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Chapter 19 Java Never Ends Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008 Pearson.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Concurrent Programming and Threads Threads Blocking a User Interface.
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
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.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Multithreading The objectives of this chapter are:
Chapter 13: Multithreading
Chapter 19 Java Never Ends
Threads Chate Patanothai.
Comp 249 Programming Methodology
Threads and Multithreading
Threads and Multithreading
Multithreaded Programming
Threads and Multithreading
Threads and Multithreading
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
21 Threads.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads.
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

Multithreading In Java, programs can have multiple threads A thread is a separate computation process Threads are often thought of as computations that run in parallel Although they usually do not really execute in parallel Instead, the computer switches resources between threads so that each one does a little bit of computing in turn Modern operating systems allow more than one program to run at the same time An operating system uses threads to do this

Thread.sleep Thread.sleep is a static method in the class Thread that pauses the thread that includes the invocation It pauses for the number of milliseconds given as an argument Note that it may be invoked in an ordinary program to insert a pause in the single thread of that program It may throw a checked exception, InterruptedException, which must be caught or declared Both the Thread and InterruptedException classes are in the package java.lang

A Nonresponsive GUI The following program contains a simple GUI that draws circles one after the other when the "Start" button is clicked There is a 1/10 of a second pause between drawing each circle If the close-window button is clicked, nothing happens until the program is finished drawing all its circles Note the use of the Thread.sleep (in the method doNothing ) and getGraphics (in the method fill ) methods

Nonresponsive GUI

If the close-window button is clicked, nothing happens until the program is finished drawing all its circles ! Because the method fill is invoked in the body of the method actionPerformed, the method actionPerformed does not end until after the method fill ends Until the method actionPerformed ends, the GUI cannot respond to anything else Nonresponsive GUI

Fixing a Nonresponsive Program Using Threads This is how to fix the problem: Have the actionPerformed method create a new (independent) thread to draw the circles Once created, the new thread will be an independent process that proceeds on its own Now, the work of the actionPerformed method is ended, and the main thread (containing actionPerformed ) is ready to respond to something else If the close-window button is clicked while the new thread draws the circles, then the program will end

The Class Thread In Java, a thread is an object of the class Thread Usually, a derived class of Thread is used to program a thread The methods run and start are inherited from Thread The derived class overrides the method run to program the thread The method start initiates the thread processing and invokes the run method

Threaded Version of FillDemo Without Threads With Threads

The new program uses a main thread and a second thread to fix the nonresponsive GUI It creates an inner class Packer that is a derived class of Thread The method run is defined in the same way as the previous method fill Instead of invoking fill, the actionPerformed method now creates an instance of Packer, a new independent thread named packerThread The packerThread object then invokes its start method The start method initiates processing and invokes run Threaded Version of FillDemo

Threaded Version of FillDemo (Part 4 of 6)

The Runnable Interface Another way to create a thread is to have a class implement the Runnable interface The Runnable interface has one method heading: public void run(); A class that implements Runnable must still be run from an instance of Thread This is usually done by passing the Runnable object as an argument to the thread constructor

The Runnable Interface: Suggested Implementation Outline public class ClassToRun extends SomeClass implements Runnable {... public void run() { // Fill this as if ClassToRun // were derived from Thread }... public void startThread() { Thread theThread = new Thread(this); theThread.start(); }... }

FillDemo with Runnable interface

The end