<?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 Synchronization</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-interview-questions/thread-synchronization/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, 30 Jul 2010 06:00:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>What is Thread Synchronization</title>
		<link>http://www.bestjavainterviewquestions.com/what-is-thread-synchronization/</link>
		<comments>http://www.bestjavainterviewquestions.com/what-is-thread-synchronization/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 18:22:05 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Thread Synchronization]]></category>
		<category><![CDATA[thread syncroniztion]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=459</guid>
		<description><![CDATA[THREAD SYNCHRONIZATION There will be no problem, Until when the thread uses their own data and method provided inside their run() methods. What happens when they try to use data and methods outside themselves, On that Occasions, they may compete for the same resources and may lead to serious problems. For example, one thread may [...]]]></description>
			<content:encoded><![CDATA[<p><span style="text-decoration: underline;"><strong>THREAD SYNCHRONIZATION</strong></span></p>
<p>There will be no problem, Until when the thread uses their own data and method provided inside their run() methods.</p>
<p>What happens when they try to use data and methods outside themselves,</p>
<p>On that Occasions, they may compete for the same resources and may lead to serious problems. For example, one thread may try to read a record from a file while another is still writing to the same file . Depending on the situation, we may get strange results. Java enables us to overcome this problem using a technique known as <em>synchronization</em>.</p>
<p>In case of Java, the keyword <strong>synchronized </strong>helps to solve problems by keeping a watch on such locations. For example, the method that will read information from a file and the method that will update the same file may be declared as synchronized. Example:</p>
<p><em><strong>Syntax:</strong></em></p>
<p>synchronized void update()<br />
{<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.   //code here is synchronized<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.<br />
}</p>
<p>When we declare a method synchronized, Java creates a &#8220;monitor&#8221; and hands it over to the thread that calls the method first time. As long as the thread holds the monitor, no other thread can enter the synchronized section of code. A monitor is like a key and the thread that holds the key can only open the lock.</p>
<p>It is also possible to mark a block of code as <strong>synchronized</strong> as shown below.</p>
<p>synchronized (lock-object)<br />
{<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.   //statements to be synchronized<br />
&#8230;&#8230;&#8230;&#8230;&#8230;.<br />
}</p>
<p>(OR) Simply to Say</p>
<p>A Thread Synchronization is a Coordination between different threads is known as thread synchronization. When two or more threads need to operate on the same pieces of the same data structure or their exist a producer/consumer relationship among the threads, then these threads need to be synchronized.</p>
<p>A synchronized method will not allow multiple threads in it.</p>
<p><strong><em>Sample example for thread synchronized </em></strong></p>
<p>//This program uses a synchronized block</p>
<p>class Callme {<br />
void call(String msg) {<br />
System.out.print(&#8220;["+msg);<br />
try {<br />
Thread.sleep(1000);<br />
}catch(InterruptedException e)<br />
{ System.out.println("Interrupted"); }<br />
System.out.println("]&#8220;);<br />
}<br />
}<br />
class Caller implements Runnable {<br />
String msg;<br />
Callme target;<br />
Thread t;<br />
public Caller(Callme targ, String s) {<br />
target = targ;<br />
msg =s;<br />
t=new Thread(this);<br />
t.start();<br />
}</p>
<p>//synchronize call to call()<br />
public void run() {<br />
synchronized(target) {  //synchronized block<br />
target.call(msg);<br />
}<br />
}<br />
}<br />
class Synch1{<br />
public static void main(String args[]) {<br />
Callme target=new Callme();<br />
Caller ob1 = new Caller(target, &#8220;Hello&#8221;);<br />
Caller ob2 = new Caller(target, &#8220;Synchronized&#8221;);<br />
Caller ob3 = new Caller(target, &#8220;World&#8221;);</p>
<p>//wait for threads to end<br />
try {<br />
ob1.t.join();<br />
ob2.t.join();<br />
ob3.t.join();<br />
}catch(InterruptedException e) {<br />
System.out.println(&#8220;Interrupted &#8220;);<br />
}<br />
}<br />
}</p>
<p>Here, the <strong>call(</strong>) method is not modified by <strong>synchronized.</strong> Instead, the <strong>synchronized</strong> statement is used inside Caller&#8217;s <strong>run()</strong> method. This causes the same correct output as the preceding example, because each thread waits for the prior one to finish before proceeding.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/naming-conventions-in-java/" title="Naming Conventions in Java"><img src="Array" alt="Naming Conventions in Java" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-nullpointerexception/" title="Define NullPointerException"><img src="Array" alt="Define NullPointerException" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/difference-between-sessionsave-and-sessionsaveorupdate/" title="Difference between session.save() and session.saveOrUpdate()"><img src="Array" alt="Difference between session.save() and session.saveOrUpdate()" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-cookies/" title="Define Cookies"><img src="Array" alt="Define Cookies" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/making-a-frame-non-resizable-in-java/" title="Making a Frame Non-Resizable in Java "><img src="Array" alt="Making a Frame Non-Resizable in Java " /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/what-is-thread-synchronization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
