Title: Florida Community College at Jacksonville
1Florida Community College at Jacksonville
Chapter 7 Threads Part 1
COP 2551 Object-Oriented Programming Spring,20
03
2Florida Community College at Jacksonville
- Overview
- Why using Thread?
- What is Thread?
- Thread and Process
- How to use Thread in Java?
- How to control Thread in Java? (covered by part
2)
COP 2551 Object-Oriented Programming Threads
2 of 20 slides
3Florida Community College at Jacksonville
- Why using Thread?
- There are times when a program needs to do
multiple tasks concurrently. For example, The Web
browser is multithreaded, allowing this
flexibility. - Performing more than one job is similar to having
more than one computer.(Virtual CPU) - A simplistic view of a computer is that it has a
CPU that performs computations, read-only memory
(ROM) which contains the program that the CPU
executes, and random-access memory (RAM) which
holds the data on which the program operates. - Multithreading is useful even in programs running
on a single-processor computer. In this scenario,
thread two runs while thread one is blocked
waiting for I/O. The I/O can take place in
parallel with the CPU running the other thread.
In a single-threaded case, thread two would have
to wait until thread one finished.
COP 2551 Object-Oriented Programming Threads
3 of 20 slides
4Florida Community College at Jacksonville
- What is Thread?
- A thread is is composed of three main parts
- A virtual CPU (In Java programming, the virtual
CPU is encapsulated in an instance of the Thread
class. When a thread is constructed, the code and
the data that define its context are specified by
the object passed to its constructor.) - The code the CPU is executing (Code can be shared
by multiple threads, independent of data.) - The data on which the code works (data may or may
not be shared by multiple threads, independent of
code. ) - At any moment, at most one object is executing
per CPU, while others might be waiting for
resources, or waiting for a chance to execute, or
sleeping, or dead.
COP 2551 Object-Oriented Programming Threads
4 of 20 slides
5Florida Community College at Jacksonville
- Thread and Process
- A process gives a program a place to live,
allowing access to memory and resources. A
process provides a level of isolation that keeps
different applications from inadvertently
interacting with each other. The goal of this
isolation is to increase the overall stability of
the system. - Every process contains one or more threads. You
can think of a thread as the moving part of the
process. Without a thread interacting with
elements within a process, nothing interesting
will happen. - Threads are paths of execution. The threads
perform the operations while the process provides
the isolation. A single-threaded application has
only one thread of execution.
COP 2551 Object-Oriented Programming Threads
5 of 20 slides
6Florida Community College at Jacksonville
- Application Loading
- How an application is loaded into a process
- When an executable is launched, the OS creates a
process for the executable to run in. - The OS then loads the executable into the
processs memory and looks for an entry point, a
specially marked place to start carrying out the
instructions contained within the executable. - Once the entry point is identified, a thread is
created and associated with the process. The
thread is started, executing the code located at
the entry point. From that point on the thread
follows the series of instructions. This first
thread is referred to as the main thread of the
process. - Example Windows Task Manager
COP 2551 Object-Oriented Programming Threads
6 of 20 slides
7Florida Community College at Jacksonville
- Some MultiThreading Terms
- Only one thread can be executing at a given point
in time and all other threads are waiting for
their turn to execute. When more than one
application is executing, there must be some
means of determining whose turn it is to execute.
This is generally referred to as scheduling. - A single processor executes one thread at a time.
When the processor decides that a different
thread should be executed, it saves the
information the current thread requires to
continue and switches to a different thread. This
is called a context switch.
COP 2551 Object-Oriented Programming Threads
7 of 20 slides
8Florida Community College at Jacksonville
- Thread in Java - Overview
- Javas thread support resides in three places
- The java.lang.Thread class
- The java.lang.Object class
- Java language and JVM
- Most support resides in the Thread class.
- Thread support methods in java.lan.Object class
are wait(), notify() and notifyAll(), these are
used to control Thread, and will be covered in
part 2.
COP 2551 Object-Oriented Programming Threads
8 of 20 slides
9Florida Community College at Jacksonville
- Thread in Java - Thread Class
- Some important methods
- start() Causes this thread to begin execution
the Java Virtual Machine calls the run method of
this thread. - run() If this thread was constructed using a
separate Runnable run object, then that Runnable
object's run method is called otherwise, this
method does nothing and returns. - join() Waits for this thread to die.
- Constructors and other methods see API Document
COP 2551 Object-Oriented Programming Threads
9 of 20 slides
10Florida Community College at Jacksonville
- What a Thread Executes
- To make a thread execute, you call its start( )
method. - This registers the thread with thread scheduler.
- The scheduler can be either JVM or the host OS.
- Note calling start( ) doesnt immediately cause
the thread to run it just makes it eligible to
run. - When a thread get a change to executed by CPU, it
is the method run( ) being executed. - The thread can execute its own run( ) method
- The thread can execute the run( ) method of some
other object.
COP 2551 Object-Oriented Programming Threads
10 of 20 slides
11Florida Community College at Jacksonville
- Threads Own run() Method
- To execute its own run( ) method, you need to
subclass the Thread class and Override run( )
method. - Example
- public class CounterThread extends Thread
- public void run()
- for(int I0Ilt10I)
- System.out.println(Counting I)
-
- To invoke this thread, construct an instance of
CounterThread class and then invoke its start( )
method - CounterThread ct new CounterThread()
- ct.start()
- It is legal to call run(), but nothing would
happen, and youre still performing the task in
current thread. So let the scheduler call run()
method.
COP 2551 Object-Oriented Programming Threads
11 of 20 slides
12Florida Community College at Jacksonville
- Executing Other Objects
- You have to call the Thread constructor to
specify which object owns the run( ) method that
you want. - Use Runnable interface in Thread constructor.
- public Thread(Runnable target)
- Example
- public class DownCounter implements Runnable
- public void run()
- for(int I10Igt1I--)
- System.out.println(Counting Down I)
-
- To invoke this thread
- DownCounter dc new downCounter()
- Thread t new Thread(dc)
- t.start()
COP 2551 Object-Oriented Programming Threads
12 of 20 slides
13Florida Community College at Jacksonville
- Runnable Interface
- The Runnable interface should be implemented by
any class whose instances are intended to be
executed by a thread. The class must define a
method of no arguments called run. - public void run( ) // You can only override
this. - In addition, Runnable provides the means for a
class to be active while not subclassing Thread.
In most cases, the Runnable interface should be
used if you are only planning to override the
run() method and no other Thread methods.
COP 2551 Object-Oriented Programming Threads
13 of 20 slides
14Florida Community College at Jacksonville
- Comparison of Two Methods
- Use interface implements Runnable allows the
object which you want to thread can have access
of all private member data. - Use class extends Thread will restrict you to
inherit from another class. (single inheritance
rule). - Use extends Thread is like to say This class is
a thread - Use implements Runnable, which says this class
is associated with a thread. From OO design
point of view, make it a better design.
COP 2551 Object-Oriented Programming Threads
14 of 20 slides
15Florida Community College at Jacksonville
- When Executions Ends
- When the run( ) method returns, the thread has
finished its task and is considered dead. - Once a thread is dead, it may not be started
again - if you want the threads task to be performed
again, you have to construct and start a new
thread instance. - The dead thread continues to exist you can still
access its data and call its methods (think about
it as a normal object). You just cant make it
run again. - Dead Thread property
- You can NOT restart a dead thread.
- You CAN call the methods of a dead thread.
COP 2551 Object-Oriented Programming Threads
15 of 20 slides
16Florida Community College at Jacksonville
- Kill a Thread
- You can kill the thread by forcing it into the
dead state. - Before JDK 1.2, stop() method is used. But
because is can cause data corruption or deadlock,
this method is deprecated, you should call
interrupt() instead.
COP 2551 Object-Oriented Programming Threads
16 of 20 slides
17Florida Community College at Jacksonville
- Performance Consideration
- It is a expensive operation to create, use and
discard threads - It is better to create a pool of reusable worker
threads that can be assigned chores as needed.
COP 2551 Object-Oriented Programming Threads
17 of 20 slides
18Florida Community College at Jacksonville
- Thread States
- Thread states are
- Running The state that all threads aspire to
- Various waiting states Waiting, Sleeping,
Suspended(deprecated), Blocked - Ready Not waiting for anything except the CPU
- Dead all done
- Only thread scheduler can move a ready thread
into the CPU.
COP 2551 Object-Oriented Programming Threads
18 of 20 slides
19Florida Community College at Jacksonville
- Thread Priority
- Every thread has a priority
- The priority is an integer from 1 to 10 (with 10
being highest) - threads with higher priority get preference over
threads with lower priority. - If there is more than one waiting thread with the
same priority, the scheduler chooses one of them
with no guarantee that the thread chosen will be
the one that has been waiting the longest.
COP 2551 Object-Oriented Programming Threads
19 of 20 slides
20Florida Community College at Jacksonville
- Thread Priority(cont.)
- Thread class provides some constant related to
priority - MAX_PRIORITY (10)
- NORM_PRIORITY (5)
- MIN_PRIORITY (1)
- The default priority is 5.
- How thread priority affect the scheduler are
platform dependent. - Example to set/get thread priority
- int oldPriority theThread.getPriority()
- int new newPriority Math.min(oldPriority 1,
Trhead. MAX_PRIORITY) - theThread.setPriority(newPriority )
COP 2551 Object-Oriented Programming Threads
20 of 20 slides