Home Java Java-exception Java Exception Thread

Related Tutorials


 
 

Share on Google+Share on Google+

Java Exception Thread

Advertisement
Thread is the independent path of execution run inside the program. Many Thread run concurrently in the program.

Java Exception Thread

     

Thread is the independent  path of execution run inside the program. Many Thread run concurrently in the program. Multithread are those group of more than one thread  that  runs concurrently in a program. Thread in a program is imported from java.lang.thread class.InMultithread,the thread run concurently,synchronous or asynchronous.

Advantage of Multithread

1)Multithread are lightweight as compared to any processes.

2)Context Switching between the thread is less expensive  as compared to processes.

3)Intercommunication between thread is relatively economically than processes.

4)Multithread occupy the same address and data  space.

5)Thread can run independently in program.

Method in Object  and Thread Class

Object Thread 
1)Notify( ) 1)Sleep( )
2)Notify all( ) 2)Yield( )
3)wait( )

How to Create Thread

There are method to create thread

1)Extends the Threads Class( java.lang.thread)

2)Implement Runnable interface( java .lang. thread)

Understand Exception in Threads.

1.A class name RunnableThread  implements the Runnable interface gives you  the run( ) method  executed by the thread. Object of this class is runnable

2. The Thread constructor is used to create an object of RunnableThread class by passing runnable object as parameter.. The Thread object has a Runnable object that implements the run( ) method.

3. The start( ) method is invoked on the Thread object . The start( ) method returns immediately once  a thread has been spawned.

4. The thread ends when the run( ) method ends which is to be normal termination or caught exception.

 5.runner = new Thread(this,threadName) is used to create a new thread

6 .runner. start( ) is used to start the new thread.

7.public void run( ) is overrideable method used to display the information of particular thread

8.Thread.currentThread().sleep(2000) is used to deactivate the thread untill the next thread started execution or used to delay the current thread.

Source Code

class RunnableThread implements Runnable
    {

	Thread runner;
	public RunnableThread() {
    }
	public RunnableThread(String threadName)
    {
		runner = new Thread(this, threadName);
		System.out.println(runner.getName());
		runner.start(); // (2) Start the thread.
    }
	public void run() {
		//Display info about this particular thread
		System.out.println(Thread.currentThread());
    }
   
    }

        public class RunnableExample
   {

	public static void main(String args[]) 
   {
		Thread threada = new Thread(new RunnableThread(), "threada");
		Thread threadb = new Thread(new RunnableThread(), "threadb");
		RunnableThread thread3 = new RunnableThread("thread3");
		threada.start();
		threadb.start();
	
	
			Thread.currentThread().sleep(2000);
	
              
		System.out.println(Thread.currentThread());
   }
   
   }


Output on the Command Prompt


C:\saurabh>javac RunnableExample.java
RunnableExample.java:28: unreported exception java.lang.InterruptedException; must be caught or declared to be thro
wn
Thread.currentThread().sleep(1000);
^

How to Overcome this Thread Exception in Java Program

In the preceding code, we have seen an unreported exception in threads. In order to overcome this situation, we need to place the sleep( ) method in try block in which the exception occur, followed  by the subsequent catch block to handle the try block, Now you can, Check the output on command prompt.

class RunnableThread implements Runnable {

	Thread runner;

	public RunnableThread() {
	}

	public RunnableThread(String threadName) {
		runner = new Thread(this, threadName);
		System.out.println(runner.getName());
		runner.start();
	}

	public void run() {

		System.out.println(Thread.currentThread());
	}

}

public class RunnableExample {

	public static void main(String args[]) {
		Thread threada = new Thread(new RunnableThread(), "threada");
		Thread threadb = new Thread(new RunnableThread(), "threadb");
		RunnableThread thread3 = new RunnableThread("thread3");
		threada.start();
		threadb.start();

		try {
			Thread.currentThread().sleep(2000);
		} catch (InterruptedException e) {
		}
		System.out.println(Thread.currentThread());
	}

}


Output on Command Prompt.


C:\saurabh>javac RunnableExample.java

C:\saurabh>java RunnableExample
thread3
Thread[threada,5,main]
Thread[threadb,5,main]
Thread[thread3,5,main]
Thread[main,5,main]
Advertisements

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: November 10, 2008

Related Tutorials

Discuss: Java Exception Thread  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
gfx
June 11, 2012
uudyueu

public RunnableThread(String threadName)
DMCA.com