Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.

Slides:



Advertisements
Similar presentations
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Week 5, Day 3: Observer Today Reducing coupling with the Observer The Observer pattern in Java APIs Posting events to a UI worker thread SE-2811 Slide.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Week 3, Day 1: Processes & Threads Return Quiz Processes Threads Lab: Quiz Lab 3: Strategy & Factory Patterns! SE-2811 Slide design: Dr. Mark L. Hornick.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
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 GUI programming with threads. 2 Threads and Swing Swing is not generally thread-safe: most methods are not synchronized –correct synchronization is.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Concurrent Programming and Threads Threads Blocking a User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Week 3, Day 2: Threads Questions about Threads “Multithreading” in Swing Lab tomorrow: Quiz Lab 3: Threading! SE-2811 Slide design: Dr. Mark L. Hornick.
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.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
A running program is a collection of “threads” – essentially independent execution streams When the main() method is called, the instructions within.
Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CREATING A SIMPLE GUI Mr. Crone. First GUI Today we will use Swing to create the following application:
Multithreading / Concurrency
Multithreading Lec 23.
Chapter 13: Multithreading
Java Multithreading.
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
SE-2811 Software Component Design
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.
Week 6, Class 2: Observer Pattern
Multithreading.
Constructors, GUI’s(Using Swing) and ActionListner
9. Threads SE2811 Software Component Design
Chapter 15 Multithreading
Threads and Multithreading
9. Threads SE2811 Software Component Design
Slide design: Dr. Mark L. Hornick
CMSC 202 Threads.
9. Threads SE2811 Software Component Design
Presentation transcript:

Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1

What’s a Thread? First, let’s define Process: A Process is most easily understood as a program or application running on your PC A process generally has a complete, private set of basic run-time resources, in particular: SE-28112

The JVM works with the OS to create Processes and Threads The underlying OS provides the essential multiprocessing support SE-28113

Modern operating systems are all capable of running multiple Processes simultaneously (On single-CPU PC’s) each Process runs individually for a discrete time period while one Process runs, other Processes sleep The Process currently executing changes very rapidly - every few milliseconds Operating systems use a Scheduler (basically, an Interrupt Service Routine (ISR) that executes on a timer interrupt) to distribute CPU time among Processes The net effect is that you (the user) observe all processes running simultaneously and continuously SE-28114

When you run a Java application, the JVM creates a Process and a Primary Thread The Primary Thread begins executing the main() method in the main class Note: other java programs, like applets, begin execution with an init() method If no other Threads are created, the Process terminates when the Primary Thread terminates That is, when there are no more instructions to execute on that Thread SE-28115

Threads wind their way through the code until they run out of instructions to execute public class App{ public static void main(String[] args) { App me = new App(); me.method_A(); } private void method_A() { // more code here method_B(); return; } private void method_B() { return; } private void method_C() { // more code here } SE-28116

Where do other Threads come from? 1. You implicitly create additional Threads when you write a Swing-based application Java applications that create and display windows cause Swing to create additional threads 2. You implicitly create additional Threads when you use various Java utility classes Using the Timer class causes a Thread to be created 3. You can explicitly create additional Threads and control their execution SE-28117

You already know how to create a multi-threaded app using Swing 1. Create a JFrame window containing JButtons, JTextField, etc. 2. Connect the JButtons etc to an ActionListener 3. Make the window visible Once the window is visible, a second Thread is created All calls to actionPerformed() occur on the second Thread The Event-Dispatching Thread SE-2811 Dr. Mark L. Hornick 8

Using a javax.swing.Timer is fairly straighforward: Timer timer = new Timer(timeoutPeriod, eventHandler); timer.start(); The eventHandler argument to the constructor is a reference to a class that implements Timer ActionListener That is, eventHandler contains an actionPerformed() method. This is similar to how Swing events are handled Whenever the Timer generates a timeout event, the JVM invokes actionPerformed() on another thread JVM uses the Event Dispatch thread when available; otherwise a “worker” thread is created SE-2811 Dr. Mark L. Hornick 9

Explicitly creating additional Threads is pretty easy: Thread t = new Thread( r ); t.start(); The r argument to the Thread constructor is a reference to a class that implements the Runnable interface Runnable declares a single method: public void run() When the Thread’s start() method is called, the instructions in the run() method begin executing on the new thread. The start() method returns essentially immediately; it does not wait for the started thread to finish execution. SE