Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java Exception Thread 
 

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]

                         

» View all related tutorials
Related Tags: c exception io stack type exec uri lock this block exe row handle program to execution ram each pos ast

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.