GYAN GANGA COLLAGE OFGYAN GANGA COLLAGE OF
TECHNOLOGYTECHNOLOGY
Bhoomika Thakur
Roll no:0208cs091024
2
Processes & Threads
PROCESS :
 A process consists of an execution
environment together with one or more
threads.
 An execution environment is the unit of
resource management: a collection of local
kernel managed resources to which its
threads have access.
3
Processes and Threads
Threads :
 Threads are schedulable activities
attached to processes.
 Threads can be helpful within servers:
 Concurrent processing of client’s
requests can reduce the tendency for
servers to become bottleneck.
 E.g. one thread can process a client’s request while a
second thread serving another request waits for a disk
access to complete.
4
Client & Server With Threads
Server
N threads
Input-output
Client
Thread 2 makes
T1
Thread 1
requests to server
generates
results
Requests
Receipt &
queuing
5
Processes and Threads
Processes vs. Threads
1. Processes are heavy
weighted & Threads are light
weighted.
2. Processes are expensive to
create but threads are easier to
create and destroy.
6
 Thread programming is concurrent
programming.
 Java provides methods for creating,
destroying and synchronizing threads.
 The Java thread class includes the
management methods and object
synchronization methods .
Alive
Thread State Diagram
New Thread Dead Thread
Running
Runnable
new CounterThread1(max);
run() method returns
while (…) { … }
Blocked
Object.wait()
Thread.sleep()
blocking IO call
waiting on a monitor
cntThread.start();
8
Java Thread Methods
1.setPriority(int newPriority) : Set return the thread’s
priority.
2. getPriority(): Return the thread’s priority.
3.run() : A thread executes the run() method .
4.start() : It starts the thread by calling run() method.
5.sleep(int millisecs) :Suspend a thread for a period of time.
6.yield() : Enter the READY state and invoke the scheduler.
7.destroy() : Destroy the thread.
9
Java Thread Methods
8.thread.join(int millisecs): Blocks the calling thread for up
to the specified time until thread has terminated.
9.thread.interrupt(): Interrupts thread: causes it to return
from a blocking method call such as sleep().
10.object.wait(long millisecs, int nanosecs):
Blocks the calling thread until a call made to notify() or
notifyAll() on object wakes the thread, or the thread is
interrupted, or the specified time has elapsed.
11.object.notify(), object.notifyAll()
Wakes, respectively, one or all of any threads
that have called wait() on object.
1.By creating thread class:
Class mythread extends Thread
{
}
2.By converting a class to thread:
Class myThread implements runnable
{
}
java.lang.Runnable
 An adapter which enables any given class to
operate as a thread.
 Requires one method be implemented:
 public void run()
 Most common method for adding threads
to an application.
 Prevents certain thread operations
(e.g. Thread.isAlive()).
java.lang.ThreadGroup
• When an exception is emitted by the
Thread.run method, the method
ThreadGroup.uncaughtException is called.
• Derive a class from ThreadGroup.
• Override the uncaughtException
method, relaying the thread and
exception information.
 Java provides multithreading.
 Threads are light weight ,they share the same
address space & share the same heavy wt. process.
 In java a main thread is created which controls the
other threads.
 wait() : This method tells
the calling thread to give up
the monitor & go to sleep
until some other threads
enter the same monitor &
calls notify().
 notify(): Wakes up the first
thread that called wait() on
the same object.
 notifyAll() :Wakes up all the
thread that called wait() on
the same object ,the highest
priority thread will run first.
Exception Handling
 When an exception is emitted out of the run() method,
the thread is terminated.
 The exception is consumed by the JVM because once
Thread.start() is called, the created thread is split from
the caller.
REFERENCES
&
WIKIPEDIA
QUERRIES
THANKYOU

Threads in java

  • 1.
    GYAN GANGA COLLAGEOFGYAN GANGA COLLAGE OF TECHNOLOGYTECHNOLOGY Bhoomika Thakur Roll no:0208cs091024
  • 2.
    2 Processes & Threads PROCESS:  A process consists of an execution environment together with one or more threads.  An execution environment is the unit of resource management: a collection of local kernel managed resources to which its threads have access.
  • 3.
    3 Processes and Threads Threads:  Threads are schedulable activities attached to processes.  Threads can be helpful within servers:  Concurrent processing of client’s requests can reduce the tendency for servers to become bottleneck.  E.g. one thread can process a client’s request while a second thread serving another request waits for a disk access to complete.
  • 4.
    4 Client & ServerWith Threads Server N threads Input-output Client Thread 2 makes T1 Thread 1 requests to server generates results Requests Receipt & queuing
  • 5.
    5 Processes and Threads Processesvs. Threads 1. Processes are heavy weighted & Threads are light weighted. 2. Processes are expensive to create but threads are easier to create and destroy.
  • 6.
    6  Thread programmingis concurrent programming.  Java provides methods for creating, destroying and synchronizing threads.  The Java thread class includes the management methods and object synchronization methods .
  • 7.
    Alive Thread State Diagram NewThread Dead Thread Running Runnable new CounterThread1(max); run() method returns while (…) { … } Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor cntThread.start();
  • 8.
    8 Java Thread Methods 1.setPriority(intnewPriority) : Set return the thread’s priority. 2. getPriority(): Return the thread’s priority. 3.run() : A thread executes the run() method . 4.start() : It starts the thread by calling run() method. 5.sleep(int millisecs) :Suspend a thread for a period of time. 6.yield() : Enter the READY state and invoke the scheduler. 7.destroy() : Destroy the thread.
  • 9.
    9 Java Thread Methods 8.thread.join(intmillisecs): Blocks the calling thread for up to the specified time until thread has terminated. 9.thread.interrupt(): Interrupts thread: causes it to return from a blocking method call such as sleep(). 10.object.wait(long millisecs, int nanosecs): Blocks the calling thread until a call made to notify() or notifyAll() on object wakes the thread, or the thread is interrupted, or the specified time has elapsed. 11.object.notify(), object.notifyAll() Wakes, respectively, one or all of any threads that have called wait() on object.
  • 10.
    1.By creating threadclass: Class mythread extends Thread { } 2.By converting a class to thread: Class myThread implements runnable { }
  • 11.
    java.lang.Runnable  An adapterwhich enables any given class to operate as a thread.  Requires one method be implemented:  public void run()  Most common method for adding threads to an application.  Prevents certain thread operations (e.g. Thread.isAlive()).
  • 12.
    java.lang.ThreadGroup • When anexception is emitted by the Thread.run method, the method ThreadGroup.uncaughtException is called. • Derive a class from ThreadGroup. • Override the uncaughtException method, relaying the thread and exception information.
  • 13.
     Java providesmultithreading.  Threads are light weight ,they share the same address space & share the same heavy wt. process.  In java a main thread is created which controls the other threads.
  • 14.
     wait() :This method tells the calling thread to give up the monitor & go to sleep until some other threads enter the same monitor & calls notify().  notify(): Wakes up the first thread that called wait() on the same object.  notifyAll() :Wakes up all the thread that called wait() on the same object ,the highest priority thread will run first.
  • 15.
    Exception Handling  Whenan exception is emitted out of the run() method, the thread is terminated.  The exception is consumed by the JVM because once Thread.start() is called, the created thread is split from the caller.
  • 16.
  • 17.
  • 18.