Threads in Java

Threads in Java help in multitasking. They can stop or suspend a specific running process and start or resume the suspended processes. This helps in increasing the speed of the processes. In Java programming, Java Virtual Machine (JVM) controls the life-cycle of thread. Different states of Thread during its life-cycle are New State, Ready-to-Run, Running and Dead (Terminated) etc.


 

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

Threads in Java help in multitasking. They can stop or suspend a specific running process and start or resume the suspended processes. This helps in increasing the speed of the processes.

In Java programming, Java Virtual Machine (JVM) controls the lifecycle of thread.

Different states of Thread during its lifecycle are:

  1. New State: Before the start() method invocation, thread remains in this state as it has been created but it has not been initialized
  2. Ready-to-Run: After the start() method invocation, the thread enters in this stage and is ready-to-run whenever the processor gives it a go.
  3. Running: The scheduler selects the thread from Ready-to-Run pool and CPU allots the time slot and thread is being executed
  4. Dead (Terminated): The thread has been terminated and it cannot run again

While the thread is in Running state, it can further enter into non-runnable states:

  • Sleeping: A thread here sleeps for a specified amount of time but can return to running state.
  • Blocked for Join Completion: A thread in this state is waiting for the completion of another thread.
  • Blocked for I/O: A thread here is waiting for I/O resource completion and can re-enter into Running state after availability of resources.
  • Waiting for notification: A thread here waits for notification from another thread and once it gets the notification it comes back to Running state.
  • Blocked for lock acquisition: A thread in this state is waiting to acquire the lock of an object.

Methods of Thread

Method  Return Type  Description
 currentThread( )  Thread  Returns an object reference to the thread in which it is invoked.
 getName( )  String  Call the name of the thread
 start( )  void  Starts the thread
 run( )  void  This method starts the execution of a thread
 sleep( )  void  Suspends a thread for a specified time
  isAlive( )  boolean  This method determines whethers a thread is running or not
 activeCount( )  int This method returns the number of active threads in a particular group and subgroups
 interrupt( )  void  This method interrupt the threads on which it is invoked.
 yield( )  void  This method pauses the execution of current thread temporarily and allows other threads to execute.

Example of Threads in Java:

public class Threads{
  public static void main(String[] args){
  Thread th = new Thread();
  System.out.println("Numbers are printing line by line after 5 seconds : ");
  try{
  for(int i = 1;i <= 10;i++)
    {
  System.out.println(i);
  th.sleep(5000);
  }
  }
  catch(InterruptedException e){
    System.out.println("Thread interrupted!");
  e.printStackTrace();
  }
  }
}

Advertisements

Ads
Share on Google+Share on Google+

Threads in Java

Posted on: April 20, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Threads in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads