<?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>Ankit Virparia &#187; Multithreading</title>
	<atom:link href="http://ankit.co/category/tutorials/java-tutorials/multithreading/feed" rel="self" type="application/rss+xml" />
	<link>http://ankit.co</link>
	<description>A Programmer, Designer and Trainer</description>
	<lastBuildDate>Sun, 11 May 2014 04:15:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Testing Random Execution &#8211; Java Multithreading</title>
		<link>http://ankit.co/tutorials/java-tutorials/multithreading/testing-random-execution-java-multithreading?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-random-execution-java-multithreading</link>
		<comments>http://ankit.co/tutorials/java-tutorials/multithreading/testing-random-execution-java-multithreading#comments</comments>
		<pubDate>Tue, 05 Feb 2013 08:57:18 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Multithreading]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=619</guid>
		<description><![CDATA[<p>Random Execution Multithreading is used to divide a program in to several parts which can be executed in parallel. Java supports multithreading which is absolutely true but Java as a programming language cannot execute the instruction in microprocessor in parallel(by switching). Java relies on operating system for that. So, what we are going to prove [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/testing-random-execution-java-multithreading">Testing Random Execution &#8211; Java Multithreading</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>Random Execution</h2>
<p>Multithreading is used to divide a program in to several parts which can be executed in parallel. Java supports multithreading which is absolutely true but Java as a programming language cannot execute the instruction in microprocessor in parallel(by switching). Java relies on operating system for that. So, what we are going to prove here is: Execution/Switching of the threads are done by Operation System at run time. So there will be random execution sequence for us every time we run the program. You can see the output displayed below:</p>
<pre class="brush: java; title: ; notranslate">
class RandomExecution implements Runnable
{
	Thread t;
	RandomExecution()
	{
		t = new Thread(this);
		t.start();
		for(int i=0;i&lt;10;i++)
		{
			System.out.println(&quot;Parent:&quot;+i);	
		}	
			
	}
	public static void main(String[] s)
	{
		RandomExecution obj = new RandomExecution();	
	}	
	public void run()
	{
		for(int i=0;i&lt;10;i++)
		{
			System.out.println(&quot;Run:&quot;+i);	
		}	
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/RandomExecution.jpg" rel="attachment"><img class="size-full aligncenter" alt="Random printing example" src="../../../images/RandomExecution_400.jpg" /></a><br />
<br/></p>
<hr/>
<br/><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/RandomExecution2.jpg" rel="attachment"><img class="size-full aligncenter" alt="Random printing example" src="../../../images/RandomExecution2_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/testing-random-execution-java-multithreading">Testing Random Execution &#8211; Java Multithreading</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/multithreading/testing-random-execution-java-multithreading/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Multithreading</title>
		<link>http://ankit.co/tutorials/java-tutorials/multithreading/introduction-to-multithreading?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introduction-to-multithreading</link>
		<comments>http://ankit.co/tutorials/java-tutorials/multithreading/introduction-to-multithreading#comments</comments>
		<pubDate>Sat, 12 Jan 2013 19:26:46 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Multithreading]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=482</guid>
		<description><![CDATA[<p>What is Multitreading? Multithreading is the ability of an operating system to execute different parts of a program (Thread) simultaneously. In case of single threaded application when we invoke a method, at that time the caller waits for the called function. And it pauses the execution of caller function. So a normal method call is [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/introduction-to-multithreading">Introduction to Multithreading</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>What is Multitreading?</h2>
<p>Multithreading is the ability of an operating system to execute different parts of a program (Thread) simultaneously.</p>
<p>In case of single threaded application when we invoke a method, at that time the caller waits for the called function. And it pauses the execution of caller function. So a normal method call is not gonna work to implement a multithreaded environment. Multithreading can be done by using a library package – java.lang.</p>
<p>Way to invoke a Thread is very simple. There is one interface named Runnable which gives you <strong>public void run()</strong> method which will be executed in parallel. But who will invoke it? Of course we cannot. Invocation part is handled by Thread class which is derived from Runnable itself. ultimately we will need both the entities. 1) Runnable 2) Thread. So there are 2 ways to have multiple threads in your application.</p>
<h2>Ways to create a Multithreaded application</h2>
<ol>
<li>
<h3>Implementing java.lang.Runnable interface</h3>
<p>In this case: As we are implementing Runnable interface, we need to compulsory write the run() method in our class. And We also need to create the object of Thread class so that threadobj.run() can be invoked.
</li>
<li>
<h3>Extending java.lang.Thread class</h3>
<p>In this case: As Thread is derived from Runnable and we are deriving our class from Thread, we are suppose to override the run() method which is blankly defined in Thread. And to invoke it, we do not need any extra object. We will just say start();
</li>
</ol>
<h2>What will be executed in parallel?</h2>
<p>There are 4 different approaches which can be followed for a multithreaded program. &#8220;Code Block 1&#8243; and &#8220;Code Block 2&#8243; will be executed in parallel.</p>
<h3>1. Runnable implementation and object of Thread in the same class</h3>
<pre class="brush: java; title: ; notranslate">
class ThreadDemo1 implements Runnable
{
	Thread t;
	ThreadDemo1()
	{
		t = new Thread(this);
		t.start();
		-----------------------------
		|							|
		|							|
		|		Code Block 1		|
		|							|
		|							|
		-----------------------------
	}	
	public void run()
	{
		-----------------------------
		|							|
		|							|
		|		Code Block 2		|
		|							|
		|							|
		-----------------------------
		
	}
	public static void main(String[] s)
	{
		new ThreadDemo1();	
	}
}
</pre>
<hr/>
<h3>2. Runnable implementation and object of Thread in the different classes</h3>
<pre class="brush: java; title: ; notranslate">
class ThreadDemo2
{
	Thread t;
	ThreadDemo2()
	{
		otherClass obj = new otherClass();
		t = new Thread(obj);
		t.start();
		-----------------------------
		|							|
		|							|
		|		Code Block 1		|
		|							|
		|							|
		-----------------------------
	}	
	
	public static void main(String[] s)
	{
		new ThreadDemo2();	
	}
}

class otherClass implements Runnable
{
	public void run()
	{
		-----------------------------
		|							|
		|							|
		|		Code Block 2		|
		|							|
		|							|
		-----------------------------
		
	}
}
</pre>
<hr/>
<h3>3. Extending Thread class</h3>
<pre class="brush: java; title: ; notranslate">
class ThreadDemo3 extends Thread
{
	ThreadDemo3()
	{
		start();
		-----------------------------
		|							|
		|							|
		|		Code Block 1		|
		|							|
		|							|
		-----------------------------
	}	
	public void run()
	{
		-----------------------------
		|							|
		|							|
		|		Code Block 2		|
		|							|
		|							|
		-----------------------------
		
	}	
	public static void main(String[] s)
	{
		new ThreadDemo3();	
	}
}
</pre>
<hr/>
<h3>4. Extending Thread in other class</h3>
<pre class="brush: java; title: ; notranslate">
class ThreadDemo4
{
	ThreadDemo4()
	{
		otherClass oc = new otherClass();
		oc.start();
		-----------------------------
		|							|
		|							|
		|		Code Block 1		|
		|							|
		|							|
		-----------------------------
	}	
	public static void main(String[] s)
	{
		new ThreadDemo4();	
	}
}
class otherClass extends Thread
{
	public void run()
	{
		-----------------------------
		|							|
		|							|
		|		Code Block 2		|
		|							|
		|							|
		-----------------------------
	}	
		
}


</pre>
<p><strong>Note:</strong> What we understood by this tutorial is: Code written in &#8220;Code Block 1&#8243; and &#8220;Code Block 2&#8243; will be executed in parallel. You might have noticed that in each tutorial we are creating an anonymous object of main class. Why? The reason is: main() method is static and we are deriving main class from Runnable/Thread which are not having static methods. That means memory for main() method will be allocated even though we do not create any object of main class but memory for non-static methods/variables will be only given when we create an object. So, in above pseudo code we have created anonymous object to do the same. Constructors are always non-static and in that we can access other non-static methods/variable.</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/introduction-to-multithreading">Introduction to Multithreading</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/multithreading/introduction-to-multithreading/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is Thread?</title>
		<link>http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-thread</link>
		<comments>http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread#comments</comments>
		<pubDate>Thu, 03 Jan 2013 09:19:38 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[GTU Core Java]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=66</guid>
		<description><![CDATA[<p>Thread is a process in execution which can be scheduled or managed. A normal Java program by default has a main thread which start executing public static void main(String[] s) method. When we launch our program by java.exe or any other launcher it is invoked. But here we are suppose to control or maintain the [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread">What is Thread?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Thread is a process in execution which can be scheduled or managed. A normal Java program by default has a main thread which start executing public static void main(String[] s) method. When we launch our program by java.exe or any other launcher it is invoked. But here we are suppose to control or maintain the single threaded application.</p>
<p>To schedule/manage any thread we must have object of Thread class. As such main thread is not created by developer we have to get the object from Runtime by calling a simple method.</p>
<pre class="brush: java; title: ; notranslate">
class mainThread
{
	public static void main(String[] s)
	{
		Thread t = Thread.currentThread();
	}
}
</pre>
<p>Now object &#8220;t&#8221; can be used to call any method of Thread class. Which will ultimately control the main Thread. e.g.</p>
<pre class="brush: java; title: ; notranslate">
class mainThread
{
	public static void main(String[] s) throws InterruptedException
	{
		Thread t = Thread.currentThread();
		System.out.println(&quot;Thread Started&quot;);
		t.sleep(1000);
		System.out.println(&quot;After Sleep&quot;);
	}
}
</pre>
<p>On calling t.sleep(1000) method the execution will be paused for 1second and then it will execute further. Method sleep(int) throws an InterruptedException which can be handled by applying &#8220;throws InterruptedException&#8221; clause.</p>
<p style="text-align: center;">If we really want to track the execution, We can print the Date object as below:</p>
<p><a class="highslide"  onclick="return hs.expand(this)" href="../../../wp-content/uploads/2013/01/DatePrint_SingleThread.bmp" rel="attachment wp-att-76"><img class="size-full wp-image-76 aligncenter" alt="Single Thread Date Printing" src="../../../wp-content/uploads/2013/01/DatePrint_SingleThread.bmp" width="470" height="238" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread">What is Thread?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
