Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1."— Presentation transcript:

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

2 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

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

4 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

5 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

6 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

7 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

8 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

9 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

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


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

Similar presentations


Ads by Google