se2811-04 MultiThreadingx
Download
Report
Transcript se2811-04 MultiThreadingx
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
Access Modifiers in Review
Access Levels
Modifier
Class
Package
Subclass
World
public
Y
Y
Y
Y
protected
Y
Y
Y
N
/*package*/
Y
Y
N
N
private
Y
N
N
N
Adapted from Oracle’s Java tutorial
http://docs.oracle.com/javase/tutorial/java/javaOO/a
ccesscontrol.html
SE-2811
Dr. Mark L. Hornick
2
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….
SE-2811
Dr. Mark L. Hornick
3
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-2811
Dr. Mark L. Hornick
4
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
5
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
6
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-2811
7
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
8
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
9
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-2811
10
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
1.
2.
3.
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
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
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
13