View RSS Feed

My Java Tips

Threads In Java

Rate this Entry
by , 11-08-2011 at 07:43 PM (893 Views)
Threads are small processes that are part of a larger process. A single process may have many threads and even if one of these threads is waiting for input from the user or is blocked or is carrying out a lengthy process, the rest of the threads can continue. An example of this can be a word processor which is a process with many threads such as the spell checker, the grammar checker, the user typing area etc.In Java, thread is just the path of execution.
In Java, threads are created by two methods.

Implementing the Runnable interface.
Extending the Thread class itself.

In both these methods, the only method that needs to be over-ridden is “ run() ”.



Many Java programmers believe that classes should be extended only if they are being enhanced or modified in some way, So if none of the Thread class' methods except run() are being over-ridden then they prefer implementing the Runnable interface.

Implementing the Runnable Interface.

Java Code:
    public class thread implements Runnable
    {
    	String name;
    	Thread t;

    	thread(String tname)
    	{
    		name=tname;
    		t=new Thread(this,name);
    		System.out.println("New Thread: "+t);
    		t.start();
    	}

    	public void run()
    	{
    		try
    		{
    			for (int i=5; i>0; i--)
    			{
    				System.out.println(name+": "+i);
    				Thread.sleep(1000);
    			}
    		}
    		catch (InterruptedException e)
    		{
    			System.out.println(name + " Interrupted");
    		}
    		System.out.println(name+" Finished");
    	}
    }

    public class driver_thread
    {
    	public static void main(String[] args)
    	{
    		thread t1=new thread("A");
    		thread t2=new thread("B");
    		thread t3=new thread("C");

    		try
    		{
    			Thread.sleep(10000);
    		}
    		catch (InterruptedException e)
    		{
    			System.out.println("Main Thread Interrupted");
    		}
    		System.out.println("Main Thread Exiting");
    	}
    }
Program Output

New Thread: Thread[A,5,main]
New Thread: Thread[B,5,main]
A: 5
New Thread: Thread[C,5,main]
B: 5
C: 5
A: 4
B: 4
C: 4
A: 3
B: 3
C: 3
A: 2
B: 2
C: 2
A: 1
B: 1
C: 1
A Finished
B Finished
C Finished
Main Thread Exiting

Code Explanation
In the thread class, First we are creating a new thread in the constructor with the name that is passed to it. Every time a new thread is created, its name is displayed. In the run method, the current thread executes once and then goes to sleep after which the next thread runs and as we can see from the output, all the CPU's are sharing the processor.
In the main thread (the thread_driver class), we make the thread to sleep for 10 seconds so that it finishes last.
Extending the Thread Class.To extend the Thread class the only thing that would be changed would be to replace the
“implements Runnable” with“extends Thread”. The remaining program would remain the same and would behave the same way.

Submit "Threads In Java" to Facebook Submit "Threads In Java" to Digg Submit "Threads In Java" to del.icio.us Submit "Threads In Java" to StumbleUpon Submit "Threads In Java" to Google

Tags: sleep, thread Add / Edit Tags
Categories
Java SE

Comments