<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Thread Tutorial</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-important-notes/thread-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Define MultiTasking in Java</title>
		<link>http://www.bestjavainterviewquestions.com/define-multitasking-in-java/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-multitasking-in-java/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 11:53:03 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[MultiTasking]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=505</guid>
		<description><![CDATA[MultiTasking : There are two types of executing the statements in MultiTasking. Executing several task at a time by the microprocessor is called MultiTasking. In MultiTasking the processor time is utilized in an optimum way The 2 type of MultiTasking are as follows. (i) Process Based MultiTasking : In this several programs are executed by [...]]]></description>
			<content:encoded><![CDATA[<p><strong>MultiTasking :</strong></p>
<p>There are two types of executing the statements in <em><strong>MultiTasking.</strong></em></p>
<p>Executing several task at a time by the microprocessor is called <em>MultiTasking.</em></p>
<p>In <em>MultiTasking </em>the processor time is utilized in an optimum way</p>
<p>The 2 type of MultiTasking are as follows.</p>
<p><strong>(i) Process Based MultiTasking : </strong></p>
<p>In this several programs are executed by the Microprocessor using RoundRobin Method.</p>
<p><strong>(ii) Thread Based MultiTasking :</strong></p>
<p>Executing different parts (methods ) of the same program at a time using thread is called<em> Thread Based MultiTasking.</em><strong><br />
</strong></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 30, 2008 -- <a href="http://www.bestjavainterviewquestions.com/explain-thread-safe-jsp-page-and-there-advantages/" title="Explain Thread-Safe JSP page and there advantages">Explain Thread-Safe JSP page and there advantages</a> (0)</li><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsp-interview-questions/" title="JSP Interview Questions">JSP Interview Questions</a> (0)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-include-and-import/" title="Difference between #include and Import">Difference between #include and Import</a> (2)</li><li>August 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/thread-examples-for-extending/" title="Thread examples for extending">Thread examples for extending</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-multitasking-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Defining Life Cycle of Thread</title>
		<link>http://www.bestjavainterviewquestions.com/defining-life-cycle-of-thread/</link>
		<comments>http://www.bestjavainterviewquestions.com/defining-life-cycle-of-thread/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 10:38:41 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[lifecycle of thread]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=143</guid>
		<description><![CDATA[When you are programming with threads, understanding the life cycle of thread is very 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 to CPU and start executing straight away. Several factors determine how it will proceed. Different states [...]]]></description>
			<content:encoded><![CDATA[<p>When you are programming with threads, understanding the life cycle of thread is very 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 to CPU and start executing straight away. Several factors determine how it will proceed.</p>
<p><strong>Different states of a thread are :</strong></p>
<p><em>&#8211;&gt;New state,</em><em> &#8211;&gt;</em><em>Runnable (Ready-to-run) state, </em><em>&#8211;&gt; </em><em>Running state,</em><em> &#8211;&gt; </em><em>Dead state,</em><em> &#8211;&gt; </em><em>Blocked </em></p>
<p><strong>Defining the different states of the theread is as follows:: </strong></p>
<p><strong> 1.  New state</strong> – After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.</p>
<p><strong> 2. Runnable (Ready-to-run) state </strong>– A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.</p>
<p><strong> 3. Running state</strong> – A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.<br />
<strong> 4. Dead state </strong>– A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.<br />
<strong> 5. Blocked</strong> &#8211; A thread can enter in this state because of waiting the resources that are hold by another thread.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/advantage-of-cookies-over-url/" title="Advantage of Cookies over URL">Advantage of Cookies over URL</a> (0)</li><li>March 24, 2009 -- <a href="http://www.bestjavainterviewquestions.com/define-hashset-class/" title="Define HashSet Class">Define HashSet Class</a> (0)</li><li>July 30, 2010 -- <a href="http://www.bestjavainterviewquestions.com/stored-procedures-for-beginners/" title="Stored Procedures for Beginners">Stored Procedures for Beginners</a> (0)</li><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-sessionsave-sessionsaveorupdate-and-sessionpersist-2/" title="Difference between session.save() , session.saveOrUpdate() and session.persist()">Difference between session.save() , session.saveOrUpdate() and session.persist()</a> (0)</li><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-httpsession-class/" title="Define HttpSession Class">Define HttpSession Class</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/defining-life-cycle-of-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Life Cycle of Thread</title>
		<link>http://www.bestjavainterviewquestions.com/life-cycle-of-thread/</link>
		<comments>http://www.bestjavainterviewquestions.com/life-cycle-of-thread/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 10:28:05 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[thread lifecycle]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=136</guid>
		<description><![CDATA[In particular,when we look at the life cycle of a thread: how to create and start a thread, some of the special things it can do while it&#8217;s running, and how to stop it. The following shows the states that a Java thread can be in during its life. It also illustrates which method calls [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="font-size: medium; color: pink;"> </span></strong></p>
<p>In particular,when we look at the life cycle of a thread: how to create and start a thread, some of the special things it can do while it&#8217;s running, and how to stop it.</p>
<p>The following shows the states that a Java thread can be in during its life. It also illustrates which method calls cause a transition to another state. This is an overview of the more interesting and common facets of a thread&#8217;s life. The remainder of this section uses the Clock applet introduced to discuss a thread&#8217;s life cycle in terms of its state.</p>
<p><strong>Creating a Thread</strong></p>
<p>The application in which an applet is running calls the applet&#8217;s start method when the user visits the applet&#8217;s page. The Clock applet creates a Thread, clockThread, in its start with the bold code shown here:</p>
<p>public void start() {<br />
if (clockThread == null) {<br />
clockThread = new Thread(this, &#8220;Clock&#8221;);<br />
clockThread.start();<br />
}<br />
}</p>
<p>After the bold statement has been executed, clockThread is in the New Thread state. When a thread is a New Thread, it is merely an empty Thread object; no system resources have been allocated for it yet. When a thread is in this state, you can only start the thread. Calling any method besides start when a thread is in this state makes no sense and causes an IllegalThreadStateException. (In fact, the runtime system throws an IllegalThreadStateException any time a method is called on a thread and that thread&#8217;s state does not allow for that method call.)</p>
<p>Notice that this&#8211;the Clock instance&#8211; is the first argument to the thread constructor. The first argument to this thread constructor must implement the Runnable interface and becomes the thread&#8217;s target. The clock thread gets its run method from its target Runnable object&#8211;in this case, the Clock instance. The second argument is just a name for the thread.</p>
<p><em> </em><strong>Starting a Thread</strong></p>
<p>Now consider the next line of code in Clock&#8217;s start method shown here in bold:</p>
<p>public void start() {<br />
if (clockThread == null) {<br />
clockThread = new Thread(this, &#8220;Clock&#8221;);<br />
clockThread.start();<br />
}<br />
}</p>
<p>The start method creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread&#8217;s run method. ClockThread&#8217;s run method is the one defined in the Clock class.</p>
<p>After the start method has returned, the thread is &#8220;running&#8221;. Yet, it&#8217;s somewhat more complex than that, a thread that has been started is actually in the Runnable state. Many computers have a single processor, thus making it impossible to run all &#8220;running&#8221; threads at the same time. The Java runtime system must implement a scheduling scheme that shares the processor between all &#8220;running&#8221; threads. (See Understanding Thread Priority for more information about scheduling.) So at any given time, a &#8220;running&#8221; thread actually may be waiting for its turn in the CPU.</p>
<p>Here&#8217;s another look at Clock&#8217;s run method:</p>
<p>public void run() {<br />
Thread myThread = Thread.currentThread();<br />
while (clockThread == myThread) {<br />
repaint();<br />
try {<br />
Thread.sleep(1000);<br />
} catch (InterruptedException e){<br />
// the VM doesn&#8217;t want us to sleep anymore,<br />
// so get back to work<br />
}<br />
}<br />
}</p>
<p>Clock&#8217;s run method loops while the condition clockThread == myThread is true. This exit condition is explained in more detail in Stopping a Thread. However, for now, know that it allows the thread, and thus the applet, to exit gracefully.</p>
<p>Within the loop, the applet repaints itself and then tells the thread to sleep for one second (1000 milliseconds). An applet&#8217;s repaint method ultimately calls the applet&#8217;s paint method, which does the actual update of the applet&#8217;s display area. The Clock paint method gets the current time, formats, and displays it:</p>
<p>public void paint(Graphics g) {<br />
// get the time and convert it to a date<br />
Calendar cal = Calendar.getInstance();<br />
Date date = cal.getTime();<br />
// format it and display it<br />
DateFormat dateFormatter = DateFormat.getTimeInstance();<br />
g.drawString(dateFormatter.format(date), 5, 10);<br />
}</p>
<p>Making a Thread Not Runnable</p>
<p>A thread becomes Not Runnable when one of these events occurs:</p>
<p>* Its sleep method is invoked.<br />
* The thread calls the wait method to wait for a specific condition to be satisifed.<br />
* The thread is blocking on I/O.</p>
<p>The clockThread in the Clock applet becomes Not Runnable when the run method calls sleep on the current thread:</p>
<p>public void run() {<br />
Thread myThread = Thread.currentThread();<br />
while (clockThread == myThread) {<br />
repaint();<br />
try {<br />
Thread.sleep(1000);<br />
} catch (InterruptedException e){<br />
// the VM doesn&#8217;t want us to sleep anymore,<br />
// so get back to work<br />
}<br />
}<br />
}</p>
<p>During the second that the clockThread is asleep, the thread does not run, even if the processor becomes available. After the second has elapsed, the thread becomes Runnable again and, if the processor becomes available, the thread begins running again.</p>
<p>For each entrance into the Not Runnable state, there is a specific and distinct escape route that returns the thread to the Runnable state. An escape route works only for its corresponding entrance. For example, if a thread has been put to sleep, then the specified number of milliseconds must elapse before the thread becomes Runnable again. The following list describes the escape route for every entrance into the Not Runnable state:</p>
<p>* If a thread has been put to sleep, then the specified number of milliseconds must elapse.<br />
* If a thread is waiting for a condition, then another object must notify the waiting thread of a change in condition by calling notify or notifyAll. More information is available in Synchronizing Threads.<br />
* If a thread is blocked on I/O, then the I/O must complete.</p>
<p>Stopping a Thread</p>
<p>A program doesn&#8217;t stop a thread like it stops an applet (by calling a method). Rather, a thread arranges for its own death by having a run method that terminates naturally. For example, the while loop in this run method is a finite loop&#8211; it will iterate 100 times and then exit:</p>
<p>public void run() {<br />
int i = 0;<br />
while (i &lt; 100) {<br />
i++;<br />
System.out.println(&#8220;i = &#8221; + i);<br />
}<br />
}</p>
<p>A thread with this run method dies naturally when the loop completes and the run method exits.</p>
<p>Let&#8217;s look at how the Clock applet thread arranges for its own death. You might want to use this technique with your applets. Recall Clock&#8217;s run method:</p>
<p>public void run() {<br />
Thread myThread = Thread.currentThread();<br />
while (clockThread == myThread) {<br />
repaint();<br />
try {<br />
Thread.sleep(1000);<br />
} catch (InterruptedException e){<br />
// the VM doesn&#8217;t want us to sleep anymore,<br />
// so get back to work<br />
}<br />
}<br />
}</p>
<p>The exit condition for this run method is the exit condition for the while loop because there is no code after the while loop:</p>
<p>while (clockThread == myThread) {</p>
<p>This condition indicates that the loop will exit when the currently exiting thread is not equal to clockThread. When would this ever be the case?</p>
<p>When you leave the page, the application in which the applet is running calls the applet&#8217;s stop method. This method then sets the clockThread to null, thereby telling the main loop in the run method to terminate:</p>
<p>public void stop() {    // applets&#8217; stop method<br />
clockThread = null;<br />
}</p>
<p>If you revisit the page, the start method is called again and the clock starts up again with a new thread. Even if you stop and start the applet faster than one iteration of the loop, clockThread will be a different thread than myThread and the loop will still terminate.</p>
<p><em>The isAlive Method:</em></p>
<p>A final word about thread state: The API for the Thread class includes a method called isAlive. The isAlive method returns true if the thread has been started and not stopped. If the isAlive method returns false, you know that the thread either is a New Thread or is Dead. If the isAlive method returns true, you know that the thread is either Runnable or Not Runnable. You cannot differentiate between a New Thread or a Dead thread. Nor can you differentiate between a Runnable thread and a Not Runnable thread.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>December 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-array-in-java/" title="Define Array in Java">Define Array in Java</a> (1)</li><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/servlet-important-questions/" title="Servlet Important Questions">Servlet Important Questions</a> (0)</li><li>April 4, 2009 -- <a href="http://www.bestjavainterviewquestions.com/define-jarwarear-file/" title="Define Jar,War,Ear file">Define Jar,War,Ear file</a> (0)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/servlet-summary/" title="Servlet Summary">Servlet Summary</a> (0)</li><li>September 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/hibernate-introduction/" title="Hibernate Introduction">Hibernate Introduction</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/life-cycle-of-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thread example for Runnable Interface</title>
		<link>http://www.bestjavainterviewquestions.com/thread-example-for-runnable-interface/</link>
		<comments>http://www.bestjavainterviewquestions.com/thread-example-for-runnable-interface/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 10:05:29 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[runnable interface in thread]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=132</guid>
		<description><![CDATA[Runnable Interface : The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implements a single method called run(), which is declared like [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Runnable Interface : </strong>The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implements a single method called <strong>run()</strong>, which is declared like this.</p>
<p>public void <strong>run()</strong></p>
<p>Inside <strong>run()</strong>,you will define the code that constitutes the new thread. It is important to understand that <strong>run() </strong>can call other methods, use other classes, and declare variables, just like the main thread can. The only difference is that <strong>run()</strong> establishes the entry point for another, concurrent thread of execution within your program. This thread will end when <strong>run() </strong>returns.</p>
<p>After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class. Thread defines several constructors. The one that we will use is shown here:</p>
<p><em>Thread(Runnable threadOb, String threadName)</em></p>
<p>In the constructor, <em>threadOb</em> is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. The name of the new thread is specified by <em>threadName</em>.</p>
<p>After the new thread is created, it will not start running until you call its <strong>start()</strong> method, which is declared within Thread. In essence, <strong>start()</strong> executres a call to <strong>run()</strong>.</p>
<p>The <strong>start()</strong> method is shown here:</p>
<p><em>void start()</em></p>
<p><em>Here is an example that creates a new thread and starts it running:</em></p>
<p><em>Ex:#1;</em></p>
<p>//Create a second thread.</p>
<p>class NewThread implements Runnable{</p>
<p>Thread t;<br />
NewThread(){</p>
<p>//Create a new, second thread<br />
t=new Thread(this,&#8221;Demo Thread&#8221;);<br />
System.out.println (&#8220;Child Thread:&#8221;+t);<br />
t.start();  //Start the thread.<br />
}</p>
<p>//This is the entry point for the second thread,</p>
<p>public void run() {<br />
try{<br />
for(int i=5;i&gt;0;i&#8211;)  {<br />
System.out.println(&#8220;Child Thread:&#8221;+i);<br />
Thread.sleep(500);<br />
}<br />
}catch(InterruptedException e){<br />
System.out.println(&#8220;Child interrupted.&#8221;);<br />
}<br />
System.out.println(&#8220;Exiting child thread.&#8221;);<br />
}<br />
}</p>
<p>class ThreadDemo {<br />
public static void main(String args[]) {<br />
new NewThread();  //create a new thread<br />
try{<br />
for(int i=5;i&gt;0;i&#8211;)  {<br />
System.out.println(&#8220;Main Thread:&#8221;+i)<br />
Thread.sleep(1000);<br />
}<br />
}catch(InterruptedException e) {<br />
System.out.println(&#8220;Main thread interrupted.&#8221;);<br />
}<br />
System.out.println(&#8220;Main thread exisiting.&#8221;);<br />
}<br />
}</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>April 4, 2009 -- <a href="http://www.bestjavainterviewquestions.com/java-interview-questions/" title="Java Interview Questions">Java Interview Questions</a> (0)</li><li>July 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/springs-interview-questions-2/" title="Springs Interview Questions">Springs Interview Questions</a> (7)</li><li>August 10, 2008 -- <a href="http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/" title="Example programs of lifecycle of servlets">Example programs of lifecycle of servlets</a> (0)</li><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-sessionsave-and-sessionsaveorupdate-2/" title="Difference between session.save() and session.saveOrUpdate()">Difference between session.save() and session.saveOrUpdate()</a> (0)</li><li>April 5, 2009 -- <a href="http://www.bestjavainterviewquestions.com/how-to-get-a-leap-year/" title="How to get a Leap Year">How to get a Leap Year</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/thread-example-for-runnable-interface/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Main Exceptions in Thread</title>
		<link>http://www.bestjavainterviewquestions.com/main-exceptions-in-thread/</link>
		<comments>http://www.bestjavainterviewquestions.com/main-exceptions-in-thread/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 09:38:03 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[interrupted exception]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=127</guid>
		<description><![CDATA[Interrupted Exception is the most important exception which frequently occurs in the thread programming EX: java.lang Class InterruptedException java.lang.Object &#124;___extended byjava.lang.Throwable &#124;___extended byjava.lang.Exception &#124;___extended byjava.lang.InterruptedException All Implemented Interfaces: Serializable public class InterruptedException extends Exception Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the [...]]]></description>
			<content:encoded><![CDATA[<p><a class="alignleft" title="Interrupted Exception" href="http://computerpreferedcourses.blogspot.com/2008/08/java-theory-and-practice-dealing-with.html" target="_blank">Interrupted Exception</a> is the most important exception which frequently occurs in the thread programming</p>
<p>EX:</p>
<p><em>java.lang<br />
Class InterruptedException</em></p>
<p><em>java.lang.Object<br />
|___extended byjava.lang.Throwable<br />
|___extended byjava.lang.Exception<br />
|___extended byjava.lang.InterruptedException</em></p>
<p>All Implemented Interfaces:<br />
Serializable</p>
<p>public class InterruptedException<br />
extends Exception</p>
<p>Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.</p>
<p>Since:<br />
JDK1.0<br />
See Also:<br />
Object.wait(), Object.wait(long), Object.wait(long, int), Thread.sleep(long), Thread.interrupt(), Thread.interrupted(), Serialized Form</p>
<p><strong> </strong></p>
<ul><strong><span style="font-size: medium;"><br />
Constructor Detail</span></strong></ul>
<p><strong></strong><br />
<strong>&#8211;&gt;InterruptedException</strong></p>
<p>public InterruptedException()</p>
<p>Constructs an InterruptedException with no detail message.</p>
<p><strong>&#8211;&gt;InterruptedException</strong></p>
<p>public InterruptedException(String s)</p>
<p>Constructs an InterruptedException with the specified detail message.</p>
<p>Parameters:<br />
s &#8211; the detail message.</p>
<p><!--[if !mso]><br />
<mce:style><!  v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} --></p>
<p class="MsoNormal"><!--[endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--> <!--[endif]--><!--[if mso &#038; !supportInlineShapes &#038; supportFields]><v:shape  id="_x0000_i1025" type="#_x0000_t75" style='width:225pt;height:36pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style="mso-element:field-end" mce_style="mso-element:field-end"></span><![endif]--></p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li>September 30, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-precompile-of-jsp-page/" title="Define Precompile of Jsp page">Define Precompile of Jsp page</a> (0)</li><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/calling-a-method-in-java/" title="Calling a Method in Java">Calling a Method in Java</a> (1)</li><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/explain-session-tracking-using-servlet/" title="Explain Session tracking using Servlet">Explain Session tracking using Servlet</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/why-does-ejb-needs-two-interfaces/" title="Why does EJB needs two interfaces">Why does EJB needs two interfaces</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-nullpointerexception/" title="Define NullPointerException">Define NullPointerException</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/main-exceptions-in-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thread examples for extending</title>
		<link>http://www.bestjavainterviewquestions.com/thread-examples-for-extending/</link>
		<comments>http://www.bestjavainterviewquestions.com/thread-examples-for-extending/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 07:48:49 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[extend thread example]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=125</guid>
		<description><![CDATA[The extending class must override the method run() mehtod. Which is the entry point of the new thread.It must also call the start() to begin execution of the new thread. Here is the proceding program rewritten to extend thread Ex#1: //create a second thread by extending Thread class NewThread extends Thread{ NewThread() { //create a [...]]]></description>
			<content:encoded><![CDATA[<p>The extending class must override the method run() mehtod. Which is the entry point of the new thread.It must also call the start() to begin execution of the new thread.<br />
<em> Here is the proceding program rewritten to extend thread</em></p>
<p><strong>Ex#1:</strong></p>
<p><em>//create a second thread by extending Thread</em></p>
<p>class NewThread extends Thread{<br />
NewThread() {<br />
//create a new,second thread<br />
super(&#8220;Demo Thread);<br />
System.out.println(&#8220;child thread:&#8221;+this);<br />
start();  <em>//start the thread</em><br />
}</p>
<p><em>//This is the entry point for the second thread.</em><br />
public void run() {<br />
try {<br />
for(int i=5;i&gt;0;i&#8211;) {<br />
System.out.println(&#8220;Child thread:&#8221;+i);<br />
Thread.sleep(500);<br />
}catch (InterruptedException e) {<br />
System.out.println(&#8220;child interrupted.&#8221;);<br />
}<br />
System.out.println(&#8220;Exiting child thread&#8221;);<br />
}<br />
}<br />
class ExtendThread{<br />
public static void main(String args[]){<br />
new NewThread(); <em> //create a new thread</em><br />
try{<br />
for(int i=5;i&gt;0;i&#8211;)  {<br />
System.out.println(&#8220;Main Thread:&#8221;+i);<br />
Thread.sleep(1000);<br />
}<br />
}catch(InterruptedException e) {<br />
System.out.println(&#8220;Main thread interrupted.&#8221;);<br />
}<br />
System.out.println(&#8220;main thread exiting&#8221;);<br />
}<br />
}</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-default-package-in-java/" title="What is Default Package in Java">What is Default Package in Java</a> (0)</li><li>July 21, 2010 -- <a href="http://www.bestjavainterviewquestions.com/how-to-make-a-class-serializable/" title="How to Make a Class Serializable">How to Make a Class Serializable</a> (1)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-are-synchronized-methods-and-synchronized-statements/" title="What are synchronized methods and synchronized statements?">What are synchronized methods and synchronized statements?</a> (0)</li><li>August 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/life-cycle-of-thread/" title="Life Cycle of Thread">Life Cycle of Thread</a> (0)</li><li>June 8, 2008 -- <a href="http://www.bestjavainterviewquestions.com/applet-interview-question/" title="Applet Interview Question">Applet Interview Question</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/thread-examples-for-extending/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Thread, How to Use Threads?</title>
		<link>http://www.bestjavainterviewquestions.com/what-is-thread-how-to-use-threads/</link>
		<comments>http://www.bestjavainterviewquestions.com/what-is-thread-how-to-use-threads/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 07:32:11 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Tutorial]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=122</guid>
		<description><![CDATA[Def. of Thread: A Thread represents a process (or) execution of a statment one by one by the Microprocessor(JVM) is called Thread. Note: Every java program will have a thread. The basic usage of the thread is as follows: In the most general sense, you can create a thread by instantiating an object of type [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Def. of Thread</strong>: A Thread represents a process (or)<br />
execution of a statment one by one by the Microprocessor(JVM) is called Thread.</p>
<p><em>Note:</em> Every java program will have a thread.</p>
<p><em>The basic usage of the thread is as follows</em>:</p>
<p>In the most general sense, you can create a thread by instantiating an object of type thread.<br />
<strong>There are two ways to create a new thread.</strong></p>
<p><strong>&#8211;&gt; You can Extend the Thread class in your class and override the method run().</strong></p>
<p><em>Syntax :</em></p>
<p>public class MyThread extends Thread<br />
{<br />
public void run()<br />
{<br />
&#8212;&#8212;<br />
}<br />
}</p>
<p><strong>&#8211;&gt; You can implement the Runnable interface and use that class in the Thread constructor</strong></p>
<p>Syntax :</p>
<p>public class MyThread implements Runnale<br />
{<br />
public void run()<br />
{<br />
&#8212;&#8212;<br />
}<br />
}</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/different-types-of-session-tracking/" title="Different types of Session Tracking">Different types of Session Tracking</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-string-and-stringbuffer/" title="Difference between String and StringBuffer">Difference between String and StringBuffer</a> (0)</li><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-port-number/" title="What is Port Number">What is Port Number</a> (0)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/thread-safety-of-the-servlet/" title="Thread safety of the servlet">Thread safety of the servlet</a> (0)</li><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-session-tracking/" title="Define Session Tracking">Define Session Tracking</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/what-is-thread-how-to-use-threads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

