Professional Documents
Culture Documents
Programming
Megha Aggarwal
IGIT, GGSIP University,
Delhi Email :
What is a Thread ?
• A form of multi tasking.
•A single sequential flow of control
• A piece of code that can execute in concurrence with
other threads.
• All programs have at least one thread – the main thread
• And these are independent executables.
• All threads are parts of a process hence communication
easier and simpler.
• A thread can belong to a thread group.
TYPES OF MULTI-TASKING
1. Process based
2. Thread based
Single Task:
Two Task:
Serial Vs. Parallel Computing
COUNTER COUNTER 2
COUNTER 1
Q
Please
Thread Vs. Process
To start a Thread:
1. Create Thread Object
2. Call its start() method
Method 1 : Subclassing Thread
Create a separate subclass that extends Thread.
- No import statement needed. Thread is in java.lang package
Reasons :
Not a big problem for getting started but a bad
habit for industrial strength development
PREEMPTION
yield () Method
Allows any other threads of same or higher
priority to execute (moves itself to the end of
priority queue)
If all waiting threads have a lower prioritiy, then
the yielding thread remains on the CPU.
Thread Life Cycle Diagram:
Class Thread and its Methods
Constructors :
Thread ()
- Allocates a new Thread object.
Thread (Runnable target)
- Allocates a new Thread object.
Thread (Runnable target, String name)
- Allocates a new Thread object.
Thread (String name)
- Allocates a new Thread object.
A few Methods
static int activeCount()
Returns the number of active threads in the current thread's thread
group.
static Thread currentThread()
Returns a reference to the currently executing thread object.
void destroy()
Destroys this thread, without any cleanup.
static int enumerate(Thread[] tarray)
Copies into the specified array every active thread in the current
thread's thread group and its subgroups.
String getName()
Returns this thread's name.
int getPriority()
Returns this thread's priority.
ThreadGroup getThreadGroup()
Returns the thread group to which this thread belongs.
A few Methods (…contd)
void interrupt()
Interrupts this thread.
boolean isAlive()
Tests if this thread is alive.
boolean isInterrupted()
Tests whether this thread has been interrupted.
void join ()
Waits for this thread to die.
void join (long millis)
Waits at most millis milliseconds for this thread to die.
void 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.
void setName(String name)
Changes the name of this thread to be equal to the argument name.
A few Methods (…contd)
void setPriority(int newPriority)
Changes the priority of this thread.
static void sleep(long millis)
Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds.
void start()
Causes this thread to begin execution; the Java Virtual Machine calls
the run method of this thread.
String toString()
Returns a string representation of this thread, including the thread's
name, priority, and thread group.
static void yield()
Causes the currently executing thread object to temporarily pause and
allow other threads to execute.
Daemon Threads
• Any Java thread can be a daemon thread.
• Daemon threads are service providers for other
threads running in the same process.
• The run() method for a daemon thread is typically an
infinite loop that waits for a service request.
• When the only remaining threads in a process are
daemon threads, the interpreter exits.
• To specify that a thread is a daemon thread, call
the setDaemon() method with the argument true. To
determine if a thread is a daemon thread, use the
method isDaemon().
Pitfalls of Thread Programming:
Method 2
Key
Block 1
Threads
Synchronization in Monitors