Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java - Threads in Java

 
 

Share on Google+Share on Google+

Java - Threads in Java

Advertisement
In this section, you will learn how to use the threads in java.

Java - Threads in Java

     

Thread is the feature of mostly languages including Java. Threads allow the program to perform multiple tasks simultaneously. Process speed can be increased by using threads because the thread can stop or suspend a specific running process and start or resume the suspended processes. Multitasking or multiprogramming is delivered through the running of multiple threads concurrently. If your computer does not have multi-processors then the multi-threads really do not run concurrently.

This example illustrates how to create a thread and how to implement the thread. In this example we will see that the program prints numbers from 1 to 10 line by line after 5 seconds which has been declared in the sleep function of the thread class. Sleep function contains the sleeping time in millisecond and in this program sleep function has contained 5000 millisecond mean 5 second time. There is sleep function must caught by the InterruptedException. So, this program used the InterruptedException which tells something the user if thread is failed or interrupted.

Here is the code of the program : 

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();
  }
  }
}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Related Tutorials

Discuss: Java - Threads in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:35
vicky
July 18, 2012
Java

threats in java
krishatha
August 5, 2012
new improvement

i looking every tutorial about java in this site.i have a problem about .its may b new idea.sml thing.bcx i decide to say every examples after the given there avnswr.its easy to understand.
komal
August 17, 2012
error in thread example

when i run the thread show error class ir imterface r excepted
zahid hussain
October 22, 2012
e.printStackTrace();

e.printStackTrace(); my means it has what kind of function
Euphamia
November 28, 2012
Creating a fan using threads

Someone tolled me to visit your page and he said i wont regret
himanshu mishra
November 27, 2013
java threads

when interrupted exception occur in java threads ?
ambika pratap
January 21, 2014
java

jdbc aur database connectivity kishe hota hai sir
raja
June 30, 2014
threads in java

plsss send all by using thread programes code
suresh kumar
August 23, 2012
java

all coding send my mail address
DMCA.com