130 likes | 212 Views
Week 3, Day 1: Processes & Threads. Retur n Quiz Processes Threads Lab: Quiz Lab 3: Strategy & Factory Patterns!. Access Modifiers in Review. Adapted from Oracle’s Java tutorial http ://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html. What SE1011 students are told….
E N D
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
Access Modifiers in Review • Adapted from Oracle’s Java tutorial http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html SE-2811 Dr. Mark L. Hornick
What SE1011 students are told… When the main() method is called, the instructions within the method begin to execute in sequence The program terminateswhen the main() method finishes executing Is this really true? Sometimes it is…. SE-2811Dr. Mark L. Hornick
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 SE-2811Dr. Mark L. Hornick
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-2811
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-2811
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 amongProcesses • The net effect is that you (the user) observe all processes running simultaneously and continuously SE-2811
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-2811
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-2811
Where do other Threads come from? • 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 • You implicitly create additional Threads when you use various Java utility classes Using the Timer class causes a Thread to be created • You can explicitly create additional Threads and control their execution SE-2811
You already know how to create a multi-threaded app using Swing • Create a JFrame window containing JButtons, JTextField, etc. • Connect the JButtons etc to an ActionListener • 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
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 TimerActionListener • 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
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-2811