Introduction:

A thread is defined as a separate stream implementation that take place simultaneously with and independently of everything else that might be happening. It does not have an event loop. A thread runs autonomously of anything else happening in the computer. With threads the other tasks that don't get stuck in the loop can continue processing without waiting for the stuck task to terminate. A thread is a coding that doesn't affect the architecture of an application. Threading is equally separate of an application. Threading is equally separate the computer's power among different tasks.


Overview of Threading

Threading concept is very important in Java Programming language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime. In Java, an object of the thread class can represent a thread can be implemented through any one of two ways: using threads in Java will enable greater flexibility of programmer looking for that extra edge in their programs. The simplicity of creating, configuring and running threads lets Java programmer's device portable and powerful applets/applications that cannot be made in other third-generation languages. Threads allow any program to perform multiple tasks at once. In an Internet-aware language such as Java, this is very important tool.


Threading Life Cycle:

When you are programming with threads understanding the life cycle of thread is very important or valuable. While a thread is alive, it is in one of several states. By invoking start () method, it doesn't mean that the thread has access of CPU and start executing straight away. Several factors determine how it will proceed.



Thread Life Cycle

1. New state - After the construction of thread instance the thread is in this state but before the  start () method invocation. At this point, the thread is considered not alive.


2. Runnable ( Read-to-run) state - A thread start its life from runnable state. Thread first runnable state after can come again to this state after either running, waiting, sleeping of coming back from blocked state also. On this state a thread is waiting for a turn on the processor.


3. Running state -  A thread is in running state that means the thread is presently expecting. There are numerous way to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.


4. Dead state - A thread can be considered dead when it's run () method completes. If any thread comes on this state that means it cannot ever run again.


5. Blocked - A thread an enter in this state because of waiting the resources that are holy by another thread.


Advantage of multithreading over multitasking:

Reduce the computation time
Improves performance of an application
Threads distribute the same address space so it saves the memory.
Context switching between threads is usually less costly than between processes.
Cost of communication between threads is comparatively low.
Thread creation and simple programs:
In Java, an object of the thread class can represent a thread. Thread can be implemented through any one of two ways:
Extending the java.lang. thread class implementing the java.lang. Runnable interface.



a. Extending Java.lang. thread class
          Syntax: class Mythread extend Thread



b. Implementing the java.lang. Runnable interface
                   Syntax: MyThread implements Runnable.
{
}
c. After declaration of thread class, we have to override run () method in class.
d. Now we can create object of thread if needed. In short we have to following these steps:


1.   Extend the java.lang. thread class


2.   Override the run () method in the subclass from the thread class to define the code executed by the thread.


3.    Create an instant of this subclass. This subclass may call a thread class constructor by subclass constructor.


4. Invoke the start () method on the instance of the class to make the thread eligible running.


The following programming demonstrate a single thread creation extending the " Thread " class.
Class MyThread extend Thread
{
Strings=null;
MyThread (string s 1)
{
S=s1:
Start ();
}
Public void run ()
{
System.out.println (s);
}
}
Public class Run Thread
{
Public class Run Thread
{
MyThread m1 = new MyThread ("Thread start....");
}
}


Output of the program is:

C:\> JAVA Run Thread. Java
C:\> Java Run Thread
Thread started....
II. Implementing the Java.lang.Runable interface The procedure for creating threads by implementing the Runnable interface is as follows:


1.  A class implements the Runnable interface, override the run () method to define the code executed by thread. An object of this class is Runnable object.


2. Create an object of thread class by passing a Runnable object as argument.


3. Invoke the start () method on the instance of the thread class.


The following program demonstrates the thread creation implementing the Runnable interface:
Class Thr 1 implements Runnable {
Thread t;
String s=null;
Thr 1(string s 1){
S=s 1;
t=new Thread (this);
t.start();
}
Public void run () {
System.out.printin (s);
}
}
Public class runnable Thread {
Public class void main (string args[ ] ) {
Thr 1 m 1 =new the 1 ("Thread started...."):
}
}


Output:

C:\> Java Runnable thread.java
C:\> Java Runnable Thread
Thread started....
However, this program returns the output same as of the output generated through the previous program there are two reasons for implementing a Runnable interface preferable to extending the Thread class:
If you extend the thread class, that means that subclass cannot extend any other class, but if you implement runnable interface then you can do this.
The class implementing the runnable interface can avoid the full overhead of thread class which can be excessive.
Join() is Alive () methods:
The following program demonstrates the join() & is  alive () methods:
Class Demo alive extends Threads {
Int value;
Public Demo Alive (string str)
{
Super  {str};
Value= ();
Start ()
}
Public void run ()
{
Try
{
While (value<5){
System.out.printin(get Name ()+ “ ” + (value++));
Thread.sleep(250);
}
} Catch (Exception e ) { }
System.out.printIn (“ Exit from thread:”+ getName ());
}
}
Public static void main (string [ ] args)
{
Demo Alive da = new Demo Alive (“thread a ”);
Demo Alive da = new Demo Alive (“thread b ”);
Try
{
System.out.printIn(“wait for the child thread to finish”);
da.join()
If (!da.isAlive())
System.out.printIn(“thread A not alive”);
db.join();
If (!db.is.Alive())
System.out.printIn(“thread B not alive”);
}Catch (Exception e) { }
System.out.printIn(“Exit from main thread ”);
}
  }


Output:

C:\> Java Demo join.java
C:\> Java Demo join
Wait for the child threads to finish.
Thread a: 0
Thread b: 0
Thread a: 1
Thread b: 1
Thread a: 2
Thread b: 2
Thread a: 3
Thread b: 3
Thread a: 4
Thread b: 4
Exit from thread: Thread a
Thread A not alive
Exit from thread: Thread b
Thread B not alive
Exit from main Thread