Home Java Thread What Is Thread In Java?
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

What Is Thread In Java?

Advertisement
This section "what is thread in java?" will describe you about threading in Java.

What Is Thread In Java?

In this section we will read about thread in Java. Here you will see the definition of thread, a very simple example of thread execution and the description of the example.

Before, defining a Thread in Java we will first focus on some points which are required to know in advance before knowing about Thread. So, lets first discuss about what is program?, what is process ?

What Is Program?

Program is a set of instructions that are executed sequentially.

What Is Process?

A process is a program that specifies how a specific task should be done. Basically, it follows a sequence for executing. It is a collection of instructions that are executed at the same time at run time . That's why more than one processes can be associated with the same program.

Thread

Definition : Thread is a process which does the special task after executing. This is a lightweight process that resides inside the program. Like, a Process more than one thread can also be associated with a single process. So, a process with single thread called single threaded process whereas, process with multiple thread is called Multi-threaded process.

Thread In Java

Thread in Java has implemented with the same meaning that actually, it is defined. Every Java program is run as a thread, thread is a sequential path of code execution within a program. Thread in Java follows a life cycle from its creation to dead. Threads can define their own variables, they have the program counter that specifies the execution of threads.

Example

Here I am giving a simple example which will demonstrate you about how to write thread in Java. Here we are going to give Java single thread example where you will see how a single thread is processed. In Java Thread can be used by two ways either by extending a Thread class or by implementing a Runnable interface. Here we will give the two examples, one will explain you that how to create a thread by extending and the another will explain you how to create thread by implementing the interface.

Thread Example using extending Thread class

MyThread.java

 class MyThread extends Thread{

  String s=null;

  MyThread(String s1){
  s=s1;
  start();
  }
  public void run(){
  System.out.println(s);
  }
}

RunThread.java

public class RunThread{
  public static void main(String args[]){
   
  MyThread m1=new MyThread("Thread Example Using Extending Thread Class");  
 }
}

Thread Example using implementing Runnable interface

RunnableThread.java

 class RunnableThread implements Runnable{
  Thread t;
  String s=null;

  RunnableThread(String s1){
   s=s1;
  t=new Thread(this);
  t.start();
  }
  public void run(){
  System.out.println(s);
 }
}

RunnableThreadExample.java

public class RunnableThreadExample{
  public static void main(String args[]){
  RunnableThread m1=new RunnableThread("Thread started....");
  }
}

Output :

When you will execute both, RunThread.java and RunnableThreadExample.java one by one you will get the same output.

Download Source Code

http://www.roseindia.net/java/thread/index.shtml

Advertisement

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 25, 2013

Ask Questions?    Discuss: What Is Thread In Java?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com