0% found this document useful (0 votes)
182 views

Multithreading in Java

The document discusses how to create and manage threads in Java, including using the Runnable interface to create a thread, the different states of a thread's lifecycle such as new, runnable, running, blocked, and terminated, and methods for controlling threads like start(), run(), sleep(), yield(), join(), isAlive(), getPriority(), and setPriority(). It also provides an example of setting different priorities for multiple threads.

Uploaded by

Raju Puppala
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Multithreading in Java

The document discusses how to create and manage threads in Java, including using the Runnable interface to create a thread, the different states of a thread's lifecycle such as new, runnable, running, blocked, and terminated, and methods for controlling threads like start(), run(), sleep(), yield(), join(), isAlive(), getPriority(), and setPriority(). It also provides an example of setting different priorities for multiple threads.

Uploaded by

Raju Puppala
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Multithreading in java

Creating a thread using Runnable


interface
1.Create a class that implements Runnable.
2.Provide a run method in the Runnable class.
3.Create an instance of the Thread class and pass
your Runnable object to its constructor as a parameter.
4.A Thread object is created that can run your Runnable class.
5.Call the Thread object’s start method.
6.The run method of your Runnable object is called and
executes in a separate thread.
class Thread2 implements Runnable }
{ }
public void run() }
{ public static void main(String args[])
for(int i=1;i<=5;i++) {
{ Thread2 t1=new Thread2();
System.out.println(Thread.currentThre Thread2 t2=new Thread2();
ad()+":"+i); Thread th1=new Thread(t1);
try{ Thread th2=new Thread(t2);
Thread.sleep(1000); th1.start();
} th2.start();
catch(Exception e) }
{ }
System.out.println(e);
Life cycle of a thread
• The life cycle of the thread in java is controlled by JVM.
• The java thread states are as follows:
1.New
2.Runnable
3.Running
4.Non-Runnable (Blocked)
5.Terminated
Life cycle of a thread

The thread is in new state if


you create an instance of
Thread class but before the
invocation of start()
method.
Life cycle of a thread

The thread is in
runnable state after
invocation of start()
method, but the thread
scheduler has not
selected it to be the
running thread.
Life cycle of a thread

The thread is in running


state if the thread
scheduler has selected it.
Life cycle of a thread

•When a thread is temporarily


inactive, then it’s in one of the
following states: when a thread is waiting for I/O to complete, it lies in the
• Blocked blocked state. It’s the responsibility of the thread
scheduler to reactivate and schedule a blocked/waiting
• Waiting
thread.
Life cycle of a thread

A thread is in the blocked state when


it tries to access a protected section of
code that is currently locked by some
other thread.
When the protected section is
unlocked, the scheduler picks one of
the thread which is blocked for that
section and moves it to the runnable
state.
Life cycle of a thread

•Terminated State: A thread


terminates because of either of the
following reasons:
•Because it exits normally. This
happens when the code of thread has
entirely executed by the program.
•Because there occurred some unusual
erroneous event, like segmentation
fault or an unhandled exception.
Thread methods
Method Description
String getName() Retrieves the name of running thread in the current context in String format
void start() This method will start a new thread of execution by calling run() method of
Thread/runnable object.
void run() This method is the entry point of the thread. Execution of thread starts from this
method.
void sleep(int This method suspend the thread for mentioned time duration in argument
sleeptime) (sleeptime in ms)
void yield() By invoking this method the current thread pause its execution temporarily and
allow other threads to execute.
void join() This method used to queue up a thread in execution. Once called on thread,
current thread will wait till calling thread completes its execution
boolean isAlive() This method will check if thread is alive or dead
Priority of a Thread (Thread Priority):

• Each thread have a priority.


• Priorities are represented by a number between 1 and 10.
• In most cases, thread schedular schedules the threads according to
their priority (known as preemptive scheduling).
• But it is not guaranteed because it depends on JVM specification that
which scheduling it chooses.
Priority of a Thread (Thread Priority):

constants defined in Thread class:


• public static int MIN_PRIORITY
• public static int NORM_PRIORITY
• public static int MAX_PRIORITY

• Default priority of a thread is 5 (NORM_PRIORITY).


• The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY
is 10.
Priority of a Thread (Thread Priority):

class TestMultiPriority1 extends Thread{     TestMultiPriority1 m2=new TestMultiPriority1(); 
 public void run(){    
   System.out.println("running thread name is:"+Th    m1.setPriority(Thread.MIN_PRIORITY);  
read.currentThread().getName());      m2.setPriority(Thread.MAX_PRIORITY);  
   System.out.println("running thread priority is:"+    m1.start();  
Thread.currentThread().getPriority());  
   m2.start();  
      
  }  
  }  
 public static void main(String args[]){   }     
  TestMultiPriority1 m1=new TestMultiPriority1(); 
 

You might also like