Java Threads Part I. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications in.

Slides:



Advertisements
Similar presentations
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Advertisements

Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
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.
Concurrency…leading up to writing a web crawler. Web crawlers.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
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.
Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
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.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
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.
Internet Software Development More stuff on Threads Paul Krause.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Field Trip #19 Animations with Java By Keith Lynn.
Concurrent Programming and Threads Threads Blocking a User Interface.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
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.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
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 Chapter 20 Java Never Ends.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
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.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1.
Threads and Multithreading
Chapter 19 Java Never Ends
Chapter 12 Exception Handling and Text IO
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
9. Threads SE2811 Software Component Design
Threads and Multithreading
OBJECT ORIENTED PROGRAMMING
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads.
OBJECT ORIENTED PROGRAMMING
CMSC 202 Exceptions.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Threads Part I

Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications in Java

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

The getGraphics Method The method getGraphics is an accessor method that returns the associated Graphics object of its calling object  Every JComponent has an associated Graphics object Component.getGraphics();

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

A Nonresponsive GUI

A Nonresponsive GUI (Cont’d)

Fixing a Nonresponsive Program Using Threads This is why the close-window button does not respond immediately:  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  Therefore, the method actionPerformed does not end until after the method fill ends  Until the method actionPerformed ends, the GUI cannot respond to anything else

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 Fixing a Nonresponsive Program Using Threads (Cont’d)

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

A Multithreaded Program that Fixes a Nonresponsive GUI The following 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 (Cont’d)