Saturday 3 August 2013

38:Threads

Learning Objectives
After completing this session, you will be able to:
‰  Define a thread
‰  Identify the thread priorities
Threads
‰  Threads are required to handle concurrent processes.
‰  Definition of threads:Single sequential flow of control within a program
‰  For simplicity, think of threads asprocesses executed by a program
‰  Example:
oOperating System
oHotJava Web browser
The following diagram shows sequential program and multi-threaded program:
Multi-Threading in Java Platform
‰  Every application has at least one thread or several, if you count "system" threads that
do things like memory management and signal handling.
‰  But from the point of view of application programmer, you start with just one thread,
called the main thread. This thread has the ability to create additional threads.
Thread Priorities
‰  Priorities determine, which thread receives CPU control and gets to be executed first.
‰  Definition: Integer value ranging from one to 10
‰  Higher the thread priority larger isthe chance of being executed first
‰  Example:
Two threads are ready to run
First thread: priority of 5, already running
Second thread = priority of 10, comes in while first thread is running
‰  Context switch:Occurs when a thread snatches the control of CPU from another
‰  When does it occur?
oRunning thread voluntarily relinquishes CPU control
oRunning thread is preempted by a higher priority thread
‰  There might occur some situations that more than one highest priority threads are
ready to run.
‰  The decision that which of the highest priority threads receives the CPU control is
dependent on the operating system.
‰  Windows 95/98/NT:Uses time-sliced round-robin
‰  Solaris:Executing thread should voluntarily relinquish CPU control
Try It Out
Problem Statement:
Write a program that illustrates the construction of threads and starting the threads.
Code:
class ThreadPriorityApp {
public static void main(String args[]) {
MyThread t1 = new MyThread("t1");
MyThread t2 = new MyThread("t2");
t1.start();
t2.start();
}
}
class MyThread extends Thread {
public void displayOutput(String s) {
System.out.println(s);
}
How It Works:
When you run the program,the output sequence will vary fromcomputer to computer. Also, the
output differs between each and every run.
After first time executionAfter second time execution
t2
t1
t1
t1
t2
t2
t1
t2
t2
t2
t1
t1
t1
t2
t2
t2
t1
t2
t1
t1
t2
t2
t1
t2
t1
t1
t1
t2
t1
t2
t2
t1
t2
t1
t2
t2
t1
t2
t1
t1
Tips and Tricks :
What are thread priorities? Is it a way that you can control scheduling?
Solution:Thread priorities might help you to influence the scheduler, but they still do not offer any
guarantee. Thread priorities are numerical values that tell the scheduler (if it cares) how important
a thread is to you. In general, the scheduler will kick a lower priority out of the running state if a
higher priority thread suddenly becomes runnable. Still, there is no guarantee. It is recommended
that you use priorities only if you want to influence performance, but never, ever rely on them for
program correctness.
Summary
‰  Once a thread is started, it will always enter the runnable state.
‰  The setPriority() method is use don Thread objects to give threads a priority of
between 1 (low) and 10 (high), although priorities are not guaranteed, and not all
JVMs recognize 10 distinct priority levels.
‰  If not explicitly set, a thread’s priority will have the same priority as the priority of the
thread that created it.
‰  The yield() method may cause a running thread to back out if there are runnable
threads of the same priority.
‰  The closest thing to a guarantee is that at any given time, when a thread is running it
will usually not have a lower priority than any thread in the runnable state. If a lowpriority thread is running when a high priority thread enters runnable, the JVM will
usually preempt the running low-priority thread and put the high-priority thread in.
Test Your Understanding
1.Which of the following are correct?
a)Threads are useful when mutuallyexclusive tasks are to be performed
b)Threads consume considerable amount of OS resources
c)Threads require synchronization when they access common resources
d)All the above
2.Which one of the following is not correct?
a)Every thread will go through the ‘waiting’ state during its lifetime
b)A thread will reach ‘dead’ state when the execution of its run() method is over
c)Every thread will start its life with the ‘new’ state
d)When the run() method of a thread is being executed, then it is said to be in
‘running’ state.
3.Name the different states in the life cycle of a thread.


No comments:

Post a Comment