<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Internet Technologies And Programming Languages</title>
	<atom:link href="http://jibitesh.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jibitesh.wordpress.com</link>
	<description>Tutorials, Customizations and Hacks</description>
	<lastBuildDate>Sat, 16 May 2009 21:25:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jibitesh.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Internet Technologies And Programming Languages</title>
		<link>http://jibitesh.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jibitesh.wordpress.com/osd.xml" title="Internet Technologies And Programming Languages" />
	<atom:link rel='hub' href='http://jibitesh.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Delete Desktop Icons</title>
		<link>http://jibitesh.wordpress.com/2009/05/16/delete-desktop-icons/</link>
		<comments>http://jibitesh.wordpress.com/2009/05/16/delete-desktop-icons/#comments</comments>
		<pubDate>Sat, 16 May 2009 15:34:00 +0000</pubDate>
		<dc:creator>jibitesh</dc:creator>
				<category><![CDATA[Look And Feel]]></category>
		<category><![CDATA[Linux, Fedora 10, Fedora 10 Desktop Customization, GConf Editor, Nautilus Desktop Icons]]></category>

		<guid isPermaLink="false">http://jibitesh.wordpress.com/?p=8</guid>
		<description><![CDATA[Some guys like their desktop to be clean of all sorts of shortcuts and icons. Generally, all the desktop shortcuts can be removed by right clicking and then clicking on delete. But this is about removing those icons whose delete option is disabled. The easier approach is to install gconf-editor and using the gui. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jibitesh.wordpress.com&amp;blog=7779904&amp;post=8&amp;subd=jibitesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Some guys like their desktop to be clean of all sorts of shortcuts and icons. Generally, all the desktop shortcuts can be removed by right clicking and then clicking on delete. But this is about removing those icons whose delete option is disabled.<br />
The easier approach is to install gconf-editor and using the gui.<br />
The second approach is to manually change from the files.</p>
<p><strong>GCONF-EDITOR</strong></p>
<p>Login into a terminal as root and type the following in the terminal<strong>.<br />
</strong></p>
<p><code><br />
$ yum install gconf-editor*<br />
<code><br />
This will ask your permission to download a couple of files, permit the download and you have gconf-editor installed.<br />
Now, go to <strong>Applications&gt;System Tools&gt;Configuration Editor</strong></p>
<div id="attachment_35" class="wp-caption aligncenter" style="width: 310px"><strong><strong><img class="size-medium wp-image-35" title="4" src="http://multithreadingjava.files.wordpress.com/2009/05/41.jpg?w=300&#038;h=254" alt="GConf-Editor" width="300" height="254" /></strong></strong><p class="wp-caption-text">GConf-Editor</p></div>
<p>Scroll down to <strong>Nautilus</strong>, and expand the drop down list. You will find an entry with the name <strong>Desktop</strong>.</p>
<div id="attachment_36" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-36" title="3" src="http://multithreadingjava.files.wordpress.com/2009/05/3.png?w=300&#038;h=239" alt="Nautilus (Gconf Editor)" width="300" height="239" /><p class="wp-caption-text">Nautilus (Gconf Editor)</p></div>
<p>Now you can un-check or check all the icons you need.</p>
<p><strong>SECOND APPROACH</strong></p>
<p>If you dont have the Fedora installation DVD or dont have internet or just for the fun of it, want to achieve all this without installing the gconf-editor, follow the steps below.</p>
<p>Open a terminal, and type</p>
<p><code></p>
<p>$ cd .gconf/apps/nautilus/desktop</p>
<p>$ vi %gconf.xml</p>
<p></code></p>
<p>The output which you get on the screen is</p>
<p><code><br />
&lt;?xml version="1.0"?&gt;<br />
&lt;gconf&gt;<br />
&lt;entry name="trash_icon_visible" mtime="1242467829" type="bool" value="<strong>true</strong>"&gt;<br />
&lt;/entry&gt;<br />
&lt;entry name="home_icon_visible" mtime="1242467814" type="bool" value="<strong>false</strong>"&gt;<br />
&lt;/entry&gt;<br />
&lt;entry name="computer_icon_visible" mtime="1242467812" type="bool" value="<strong>false</strong>"&gt;<br />
&lt;/entry&gt;<br />
&lt;entry name="computer_icon_name" mtime="1242308198" type="string"&gt;<br />
&lt;stringvalue&gt;Computer&lt;/stringvalue&gt;<br />
&lt;/entry&gt;<br />
&lt;/gconf&gt;</p>
<p><code></p>
<p>Change the boolean values to true or false to make it appear or disappear respectively.</p>
<p>Now log out and log in, and your desktop is clean of all the icons you need.<ins datetime="2009-05-16T20:37:17+00:00"></ins><ins datetime="2009-05-16T20:37:17+00:00"></ins></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jibitesh.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jibitesh.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jibitesh.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jibitesh.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jibitesh.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jibitesh.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jibitesh.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jibitesh.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jibitesh.wordpress.com&amp;blog=7779904&amp;post=8&amp;subd=jibitesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jibitesh.wordpress.com/2009/05/16/delete-desktop-icons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b8b482620ec499c4e55e13ff16c067e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jibitesh</media:title>
		</media:content>

		<media:content url="http://multithreadingjava.files.wordpress.com/2009/05/41.jpg?w=300" medium="image">
			<media:title type="html">4</media:title>
		</media:content>

		<media:content url="http://multithreadingjava.files.wordpress.com/2009/05/3.png?w=300" medium="image">
			<media:title type="html">3</media:title>
		</media:content>
	</item>
		<item>
		<title>MultiThreading In JAVA 5: Part I</title>
		<link>http://jibitesh.wordpress.com/2009/05/16/multithreading-in-java-5-part-i/</link>
		<comments>http://jibitesh.wordpress.com/2009/05/16/multithreading-in-java-5-part-i/#comments</comments>
		<pubDate>Sat, 16 May 2009 15:29:49 +0000</pubDate>
		<dc:creator>jibitesh</dc:creator>
				<category><![CDATA[Core]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java, Multi Threading, Threads, Java 5 Thread Tutorial, Java Multithreading Help, Multithreading tutorial, thread tutorial]]></category>

		<guid isPermaLink="false">http://jibitesh.wordpress.com/?p=3</guid>
		<description><![CDATA[We will be reading about a very common dread of Java, and of course its threads. Feel free to put down a comment if you feel you don&#8217;t agree with something here. I will surely take a look at it. Thanks guys! THREADS Why we need threads? Consider a scenario where you have a GUI [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jibitesh.wordpress.com&amp;blog=7779904&amp;post=3&amp;subd=jibitesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We will be reading about a very common dread of Java, and of course its threads. Feel free to put down a comment if you feel you don&#8217;t agree with something here. I will surely take a look at it. Thanks guys!</p>
<p>THREADS<br />
Why we need threads?<br />
Consider a scenario where you have a GUI application named MultiThreading. What our application does is, based on user inputs, it fetches data a database server and when the read button is clicked, the results are displayed. In a single thread model, the user executes a query which fetches a huge amount of data. Now as it is a single thread model, when the application is fetching the data if the user clicks on the read button, the application looks as if it has hung. The user is pissed, reports the application as a stupid one and your application is dumped.<br />
Another scenario, you are developing a mobile game in java. u need to update the various game attributes while the game is still going on. If this is implemented in a single thread model, the user will never play your game again. Take my word for it. Reason, the sucking animation of the game.<br />
Look around, everything is multi-threaded.</p>
<p>Why we dont need threads?<br />
The only and only reason for this is, multi-threading causes havoc in programs which otherwise would have run very smoothly. Take for example the classic banking example. Customer A has a debit card and he has also got an add-on card for B(I never said B was A&#8217;s girl friend). A&#8217;s bank account has twenty thousand. I am sorry to break the myth but we software engineers are really under-paid. Now, A really likes a Cannon camera which is worth 19000. A is really excited and doesnt think twice before deciding to swipe his credit card. Meanwhile, B was out shopping and she liked a Swaroski crystal showpiece which was worth 5000. Now comes the tricky part. A swipes his card, the card is validated, checked if it has sufficient funds and the amount is added to his credit. Just before the debit transaction is recorded in the database, B swipes the add on card, the card is validated and checked if it has sufficient funds. Now as the amount from A has not been deducted, it shows 20000 as debit limit and hence the transaction is successful. So, now you are happy that with 20000 u buy stuff worth 24000. But the guys at the bank are really angry, and they change their design and make the withdrawal part synchronized.(Here synchronized means only one thread can access the shared resource at a time.) And we are back into our poverty.</p>
<p>MAKE YOUR HANDS DIRTY<br />
Enough of theory, time to start coding. One last thing, Threads are parallel processes that you spawn to achieve more computation power.</p>
<blockquote><p>Two ways to create thread execution:<br />
1) Extend the Thread class<br />
2) Implement the Runnable interface</p></blockquote>
<p><code><br />
public class ExtendThread extend Thread{<br />
/* Declare class variables<br />
/* Have your own methods<br />
public void run(){<br />
/*This is where the new thread execution is started<br />
}<br />
</code><br />
<code><br />
public class ExtendThread implements Runnable{<br />
/* Declare class variables<br />
/* Have your own methods<br />
public void run(){<br />
/*This is where the new thread execution is started<br />
}<br />
</code></p>
<p>As you can see both the demo classes have one method in common and that is public void run(). The speciality of this method is that it acts as the main method for your new thread. This is the lowest entry in the new thread stack.</p>
<p>There are two very important methods which are inherited from the Object class. Those are the wait and notify methods. You can guess from this, how important multi threading is to Java.</p>
<p>Lets dig a little bit deeper now.<br />
<code><br />
public class Thread1 implements Runnable{<br />
public void run() {<br />
try{<br />
for( int i = 0; i &lt; 10; i++ ){<br />
System.out.println("Thread1 is running.");<br />
Thread.sleep(20);<br />
}<br />
}catch( InterruptedException ex){<br />
ex.printStackTrace();<br />
}<br />
}<br />
}<br />
</code><br />
<code><br />
public class Thread2 implements Runnable{<br />
public void run() {<br />
try{<br />
for( int i = 0; i &lt; 10; i++ ){<br />
System.out.println("Thread2 is running.");<br />
Thread.sleep(20);<br />
}<br />
}catch( InterruptedException ex){<br />
ex.printStackTrace();<br />
}<br />
}<br />
}<br />
</code></p>
<p><code><br />
public class TheMain {<br />
public static void main( String[] args ){<br />
Thread1 thread1 = new Thread1();<br />
Thread2 thread2 = new Thread2();<br />
Thread thread_sample1 = new Thread(thread1);<br />
Thread thread_sample2 = new Thread(thread2);<br />
thread_sample1.start();<br />
thread_sample2.start();<br />
}<br />
}<br />
</code></p>
<p>You expect the output to be alternate outputs of Thread1 and Thread2. But you cannot guarantee that. A good way to do it will be discussed later.</p>
<p>When you are implementing the Runnable interface the coding effort is considerably higher, but it is always a good practice to use the Runnable interface than extend the Thread class. This way you have the flexibity to extend any other class if you want. Lets say you have a small GUI program. How will u extend the Applet class if you are extending the Thread class already.</p>
<p>Some methods are new in this code example. The sleep method is a static method in the Thread class which puts the current thread into sleep. The Thread constructor in the main method takes in a Runnable object as argument.<br />
For other constructors of Thread class, consult the API.</p>
<p><strong>isAlive()</strong><br />
There is a time lag before the thread is actually executed by the JVM and after the thread has been terminated. The isAlive() method can be used to know if a Thread is running or not.</p>
<p><strong>join()</strong><br />
Lets say ihave created a new thread and continued with my work. But now after a certain amount of code has been executed i want my main thread to wait for the new thread which i had created and only after the new thread has completed its work, i want my main thread to continue. This can be done by using join()<br />
In the previous code example, add<br />
<code><br />
System.out.println("No Joining.")<br />
</code><br />
Now remove the above bit and insert the following code.<br />
<code><br />
try {<br />
thread_sample2.join();<br />
} catch (InterruptedException e) {<br />
e.printStackTrace();<br />
}<br />
System.out.println("With joining");<br />
</code></p>
<p>You will see the difference.</p>
<p><strong>currentThread()</strong>, <strong>getName()</strong> and <strong>setName()</strong><br />
Lets consider a scenario where you want to execute a certain part of code by a specific thread and not by others. Lets for example consider an example where we have three friends Raj, Tina and Hina. Now, Tina likes Raj while Raj likes Hina. Only if Tina could block the calls from Hina to Raj, she would have a chance with Raj. Now lets think from Java angle, Raj, Tina and Hina are three classes and you want to implement the above use cases. You get the thread name which is invoking the method in the Raj class and if it is Tina then block the call. The getName and setName methods help in setting and getting the name of a thread while the currentThread gives you the current thread which is executing the method.<br />
<code><br />
public class Raj{<br />
public void attendCall(){<br />
String name = Thread.currentThread().getName();<br />
if ( name.equalsIgnoreCase("Hina"))<br />
System.out.println("Call is blocked.");<br />
else<br />
System.out.println("Call accepted");<br />
}<br />
}<br />
</code></p>
<p><code><br />
public class Tina{<br />
public void makeCall(){<br />
System.out.println("Tina trying to call");<br />
Thread.currentThread().setName("Tina");<br />
new Thread1().attendCall();<br />
}<br />
}<br />
</code></p>
<p><code><br />
public class Hina{<br />
public void makeCall(){<br />
System.out.println("Hina trying to call");<br />
Thread.currentThread().setName("Hina");<br />
new Thread1().attendCall();<br />
}<br />
}<br />
</code><br />
If only i could do this in real world.<br />
There is a catch here. When extending the Thread class, you have the <strong>getName()</strong> method. If you use that, the thread name which you will get is the name of the thread which loaded the class and not the one which is calling the method. This is the reason why you have to first get the current thread( use <em>Thread.currentThread()</em>) and then use the <strong>getName()</strong> method on it.</p>
<p>One last thing before the basic part is over. You are generally asked on how the garbage collector in case of multi threading. Even after the thread has stopped, the garbage collector does not collect it because it has still not been dereferenced. But for all the other resources in the spawned thread, everything is garbage collected when the run method is completed. So dereference your thread variable to null and everything you can do is done.</p>
<p>SYNCHRONIZATION<br />
Synchronization is necessary to determine how one thread handles data which are also accessed by another thread. Consider the banking example given. It is one of the most basic things which requires thread access control. Another example can be, lets say you have a database where you want to log the transactions to your online shopping shop. But instead of the transasctional way, you want to store the details in bulk mode. Now you store the details in an array list or any other collection object. When you are bulk uploading, what about the collectio object then, it still might get updated by a new transaction. This can be avoided using synchronization.<br />
A important term here is RACE CONDITION. A race condition is defined as a race between two separate processes or threads to do an action on a common resource in which an action by one thread will corrupt the data for the thread which arrives later.<br />
In synchronization, a thread must obtain a lock to execute the synchronized block and while a thread has the lock, another thread cannot obtain the same lock. When the thread comes out of the synchronized part, it gives up the lock and the other waiting thread can access the synchronized part.</p>
<p>The lock object can be of method scope or class scope and even of local scope.<br />
We will get our hands dirty later. This is theory time. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
A very dreaded condition in multi-threading is deadlock. How do you define a deadlock? The ideal defination for it is, when two mutually wait for each other to complete a common task. I know it doesnt make sense, but the next couple of examples will explain. Lets say, we are at LandMark and the store is out of cash(Which is impossible!!). Now you have got a Java, Complete Refernce for 450 bucks and you have a 500 rupee note. The cashier is not ready to bill you because of lack of change. I have bought a Sin City comic for 150 bucks and by good luck i have the exact change. In the real world, we both talk, and bill first and this way the cashier has 50 bucks change and we both get our books. But imagine that because of some perverse rule, we cannot break the queue and there is no cash inflow for the million years. We both are stuck there for ever. This is what a dead lock is. The threads are dumb enough not to reconcile and keep waiting.<br />
Synchronization has a big negative performance impact on your application so use it carefully and intelligently. Use it only for the part where a value is being changed/updated. Synchronizing a whole method increases the chances of a deadlock. The only reason for saying this is that increasing the scope increases the chances. And dont synchronize an object whose value is being changed inside the synchronized block.</p>
<p><strong>wait()</strong>, <strong>notify()</strong> and <strong>notifyAll()</strong><br />
The funda behind using these methods is to make a thread wait till a particular condition is fulfilled. For example lets say we have two threads A and B which satisfies a dangerous race condition. So now you make A wait till it is notified by B. These two methods are inherited from the Object class and hence every object in Java has these methods. The wait and notify methods are used alongwith synchronization blocks to avoid race conditions. If you have more than one thread waiting, use the <strong>notifyAll()</strong> method.<br />
Lets say, you have a lot of threads waiting for one thread to complete its job. Now as soon as one thread completes the job, it notifies all and the all the threads wake up to get the object lock. But only one thread can acquire the lock and as soon as the other threads find out that the lock has already been obtained by another thread, it goes to sleep again. This puts unnecessary load on the jvm. This can be avoided by using an array of threads and deciding which thread to be notified. The drawback of this method is that it can be quite confusing and might result is a deadlock if not properly checked.<br />
Code Snippet with thread specific notify() calls. I have added a code which coordinates the order of execution in three threads.<br />
<code><br />
public class Locker {<br />
private Object[] target_locks = null;<br />
private static Locker target;<br />
public Locker(int number){<br />
target_locks = new Object[number];<br />
for(int i = 0; i &lt; number; i++)<br />
target_locks[i] = new Object();<br />
target = this;<br />
}<br />
public void wait( int targetNumber ){<br />
synchronized( target_locks[targetNumber] ){<br />
try {<br />
target_locks[targetNumber].wait();<br />
} catch (InterruptedException e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
}<br />
public void notify( int targetNumber ){<br />
synchronized (target_locks[targetNumber]) {<br />
target_locks[targetNumber].notify();<br />
}<br />
}<br />
public synchronized Object getLock(int number){<br />
return target_locks[number];<br />
}<br />
public static Locker getInstance(){<br />
return target;<br />
}<br />
}<br />
</code><br />
This is the locker class. As the name suggests, this is the class which stores all the lock objects. We do not call the wait and notify methods from the threads, instead we use this class for the wait and notify calls.<br />
<code><br />
public class Thread1 implements Runnable{<br />
public Locker target = Locker.getInstance();<br />
public void run() {<br />
int counter = 1;<br />
synchronized(target.getLock(0)){<br />
try{<br />
for( int i = 0; i &lt; 10; i++ ){<br />
System.out.println("Thread1 is running: " + counter);<br />
counter++;<br />
target.notify(1);<br />
if (i != 9)<br />
target.wait(0);<br />
}<br />
}catch( Exception ex){<br />
ex.printStackTrace();<br />
}<br />
}<br />
System.out.println("Thread1 is exiting.");<br />
}<br />
public void setLock(Locker target){<br />
this.target = target;<br />
}<br />
}<br />
</code><br />
<code><br />
public class Thread2 implements Runnable{<br />
public Locker target = Locker.getInstance();<br />
public void run() {<br />
int counter = 1;<br />
synchronized(target.getLock(1)){<br />
try{<br />
for( int i = 0; i &lt; 10; i++ ){<br />
System.out.println("Thread2 is running: " + counter);<br />
counter++;<br />
target.notify(2);<br />
if (i != 9)<br />
target.wait(1);<br />
}<br />
}catch( Exception ex){<br />
ex.printStackTrace();<br />
}<br />
}<br />
System.out.println("Thread2 is exiting.");<br />
}<br />
public void setLock(Locker target){<br />
this.target = target;<br />
}<br />
}<br />
</code><br />
<code><br />
public class Thread3 implements Runnable{<br />
public Locker target = Locker.getInstance();<br />
public void run() {<br />
int counter = 1;<br />
synchronized(target.getLock(2)){<br />
try{<br />
for( int i = 0; i &lt; 10; i++ ){<br />
System.out.println("Thread3 is running: " + counter);<br />
counter++;<br />
target.notify(0);<br />
if (i != 9)<br />
target.wait(2);<br />
}<br />
}catch( Exception ex){<br />
ex.printStackTrace();<br />
}<br />
}<br />
System.out.println("Thread3 is exiting.");<br />
}<br />
public void setLock(Locker target){<br />
this.target = target;<br />
}<br />
}<br />
</code><br />
<code><br />
public class ThreadCaller {<br />
public static void main( String args[] ){<br />
Locker target = new Locker(3);<br />
Thread1 thread1 = new Thread1();<br />
Thread2 thread2 = new Thread2();<br />
Thread3 thread3 = new Thread3();<br />
Thread runna1 = new Thread(thread1);<br />
Thread runna2 = new Thread(thread2);<br />
Thread runna3 = new Thread(thread3);<br />
runna1.start();<br />
runna2.start();<br />
runna3.start();<br />
}<br />
}<br />
</code><br />
Each thread has a lock associated with it. The Thread1 notifies the Thread2, Thread2 notifies Thread3 and Thread3 notifies Thread1. This way, the execution transfers from Thread1 to Thread2 and then to Thread3 and back to Thread1.<br />
The inter-thread communication can be done by setting thread priority and then using <em>notifyAll()</em>. To set the priority of a thread use the <em>setPriority(int i)</em> method of the Thread class.<br />
<code><br />
Someone write this code. I am pretty tired. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
</code><br />
To end this basic introduction, we will discuss about the two types of threads that exist in Java. The two types are daemon and user threads. The two types of threads are very similar to each other. Daemon threads are those threads which are spawned by the JVM internally to fulfil any given task. But, the user can play God here. He can change the thread from user to deamon and vice versa anytime he wishes before the thread is started.(Mind this. Once the thread starts, no changing buddy!) After the user thread has exited, the JVM checks for any running threads. IF all the threads running are daemon threads, then the JVM exits the program and terminates all the threads itself. Okie! i guess i did my part of making you understand what a daemon thread is. Now you start thinking, why the hell do i need a bloody daemon thread. Isnt getting bogged down by user threads enough. Now consider this my friend, everyone knows about the garbage collector of JVM. This is the magic bit which removes the developer&#8217;s overhead of removing obsolete objects from the memory. What if the GC was a user thread? The JVM can never terminate a user thread, so you see the JVM can never shut down after it runs our application. Now if GC is a daemon thread, the JVM checks for any user thread, as none is running, it terminates the garbage collector thread and exits. Neat! Isn&#8217;t it. Now lets see for any practical use. One think which i can think of is logging. I dont care if logging is successful or not, i want my JVM to shutdown when i exit it. But, my client wants to see the logs to check if everything is going according to the way he had specified. I create a daemon thread whose only job is to write into the logs. This provides a better abstraction layer. Now what if writing to the file is time consuming and while the daemon thread is writing the logs, the user thread exits. My client doesnt want to loose any logs. Luckily a small work around fixes this. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
Spawn a new user thread from your daemon thread which does nothing. And only after the logs have been written, terminate the user thread. This way the daemon thread also exits and it doesn&#8217;t depend on your application.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jibitesh.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jibitesh.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jibitesh.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jibitesh.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jibitesh.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jibitesh.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jibitesh.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jibitesh.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jibitesh.wordpress.com&amp;blog=7779904&amp;post=3&amp;subd=jibitesh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jibitesh.wordpress.com/2009/05/16/multithreading-in-java-5-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b8b482620ec499c4e55e13ff16c067e1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jibitesh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
