Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Content: Dr. Hornick Errors: Dr. Yoder 1

2 Access Modifiers in Review Access Levels ModifierClassPackageSubclassWorld publicYYYY protectedYYYN /*package*/YYNN privateYNNN Adapted from Oracle’s Java tutorial http://docs.oracle.com/javase/tutorial/java/javaOO/a ccesscontrol.html SE-2811 Dr. Mark L. Hornick 2

3 SE-2811 Dr. Mark L. Hornick 3 What SE1011 students are told… When the main() method is called, the instructions within the method begin to execute in sequence The program terminates when the main() method finishes executing Is this really true? Sometimes it is….

4 SE-2811 Dr. Mark L. Hornick 4 The ugly truth… When the main() method is called, the instructions within the method begin to execute in sequence on the primary thread The program terminates when the primary thread, and any additional threads, finish executing

5 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-28115

6 By default, a Process creates and executes a single primary Thread BUT: A Process can create and execute more than one Thread The JVM works with the OS to create Processes and Threads The underlying OS provides the essential multiprocessing support SE-28116

7 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-28117

8 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-28118

9 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-28119

10 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-281110

11 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 11

12 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 12

13 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-281113


Download ppt "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."

Similar presentations


Ads by Google