<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ro">
	<id>http://wiki.dcae.pub.ro/index.php?action=history&amp;feed=atom&amp;title=Concurrent_Programming_-_Threads</id>
	<title>Concurrent Programming - Threads - Revizia istoricului</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.dcae.pub.ro/index.php?action=history&amp;feed=atom&amp;title=Concurrent_Programming_-_Threads"/>
	<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;action=history"/>
	<updated>2026-05-27T17:42:09Z</updated>
	<subtitle>Istoricul versiunilor pentru această pagină din wiki</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1614&amp;oldid=prev</id>
		<title>Rhobincu: /* Race conditions - bariera */</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1614&amp;oldid=prev"/>
		<updated>2013-12-23T13:45:37Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Race conditions - bariera&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 23 decembrie 2013 13:45&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l183&quot; &gt;Linia 183:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 183:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&lt;/del&gt;Race &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;conditions&amp;#039;&amp;#039; &lt;/del&gt;- &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;bariera &lt;/del&gt;==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Race &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Conditions &lt;/ins&gt;- &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Barriers &lt;/ins&gt;==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În exemplul anterior&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;dacă considerăm două thread-uri&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;unul care pune elemente pe stivă și unul care le consumă&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;observăm că dacă oricare din thread-uri este mai rapid decât celălalt&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;se ajunge în situația în care se aruncă o excepție&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ori pentru că stiva e plină, ori pentru că &lt;/del&gt;s&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-&lt;/del&gt;a &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;golit. Aici apare ceea ce se numește &amp;#039;&amp;#039;&lt;/del&gt;race condition&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;adică există una sau două zone de program executate de două thread-uri diferite și în care unul din thread-uri trebuie să ajungă înaintea celuilalt ca programul să se desfășoare corect&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În exemplul anterior&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;dacă stiva este goală&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;atunci &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul care pune un element pe stivă trebuie să ajungă la metoda &amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; înainte ca celălalt thread să apeleze metoda &amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;în caz contrar generându-se o excepție&lt;/del&gt;.  &lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In the example above&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is there are two threads&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;one writing elements onto the stack and one consuming them&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;we can see that if any of the threads is slightly faster&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;it will end up throwing an exception&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;either because the stack gets empty or full. This is what&amp;#039;&lt;/ins&gt;s &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;called &lt;/ins&gt;a race condition&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;: one more code sections&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;executed by two or more threads that are expected to be executed in a particular order that ends up being inverted&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;So in the example&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;if the stack is empty&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;then the &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;pushing should take the monitor before the one popping&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;or an exception will be thrown&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Această problemă se rezolvă folosind un sistem de bariere&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;adică un sistem care oprește unul din &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-uri într-un anumit loc până când o condișie este îndeplinită (de cele mai multe ori&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;când alt thread ajunge în locul potrivit)&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În &lt;/del&gt;Java, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;acest lucru se realizează cu ajutorul metodelor &lt;/del&gt;&amp;#039;&amp;#039;wait()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long)&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long, int)&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;și &lt;/del&gt;&amp;#039;&amp;#039;notify()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;This problem is solved by using a barrier system&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;which is a system that stops a &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;in a certain spot&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;until a condition is met&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In &lt;/ins&gt;Java, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;this is done by calling methods &lt;/ins&gt;&amp;#039;&amp;#039;wait()&amp;#039;&amp;#039; / &amp;#039;&amp;#039;wait(long)&amp;#039;&amp;#039; / &amp;#039;&amp;#039;wait(long, int)&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;and &lt;/ins&gt;&amp;#039;&amp;#039;notify()&amp;#039;&amp;#039; / &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;belonging to class Object&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Metodele &lt;/del&gt;&amp;#039;&amp;#039;wait()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long)&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long, int)&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;blochează &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul curent până când un alt &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;apelează una din metodele &lt;/del&gt;&amp;#039;&amp;#039;notify()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;sau până când timpul dat ca &lt;/del&gt;argument a &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;expirat&lt;/del&gt;.  &lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Methods &lt;/ins&gt;&amp;#039;&amp;#039;wait()&amp;#039;&amp;#039; / &amp;#039;&amp;#039;wait(long)&amp;#039;&amp;#039; / &amp;#039;&amp;#039;wait(long, int)&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;block the current &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;until another &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;calls one of the &lt;/ins&gt;&amp;#039;&amp;#039;notify()&amp;#039;&amp;#039; / &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;methods for the same object&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;until the time given as an &lt;/ins&gt;argument &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;has expired or until another thread interrupts the waiting thread by calling &amp;#039;&amp;#039;Thread.interrupt()&amp;#039;&amp;#039;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;Rule:&amp;lt;/font&amp;gt;Calls to &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039; methods are always done in &lt;/ins&gt;a &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;synchronized section and are called for the monitor object&lt;/ins&gt;.&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/div&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;Regulă:&amp;lt;/font&amp;gt; Apelurile metodelor de tip &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039; se fac obligatoriu într-un bloc &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; și se folosește ca referință obiectul monitor:&amp;lt;/div&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l250&quot; &gt;Linia 250:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 251:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Metodele de tip &lt;/del&gt;&amp;#039;&amp;#039;notify()&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;reactivează threadurile oprite folosind metodele &amp;#039;&amp;#039;wait()&amp;#039;&lt;/del&gt;&amp;#039;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;. Acestea nu trebuie obligatoriu să fie apelate dintr-un bloc sincronizat&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;dar este recomandat&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Se folosește ca referință tot obiectul de tip &lt;/del&gt;monitor &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;utilizat pentru apelul metodelor &amp;#039;&amp;#039;&lt;/del&gt;wait&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;()&amp;#039;&amp;#039;&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Methods of type &lt;/ins&gt;&amp;#039;&amp;#039;notify()&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;reactivate waiting threads. These don&lt;/ins&gt;&amp;#039;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;t have to be called from within a synchronized section&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;but it is recommended to do so&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;They are called for the &lt;/ins&gt;monitor &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;objects used to call &lt;/ins&gt;wait &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;on the waiting threads&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Un &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;reactivat folosind &lt;/del&gt;&amp;#039;&amp;#039;notify&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;()&lt;/del&gt;&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;trebuie să aștepte eliberarea monitorului pentru a executa zona sincronizată&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ca orice alt &lt;/del&gt;thread. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Observați utilizarea acestei funcționalități în metoda &lt;/del&gt;&amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;unde s-a apelat &lt;/del&gt;&amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;înainte de extragerea elementului de pe stivă&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;pe linia următoare&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Totuși, clasa funcționează corect, pentru că &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul care introduce pe stivă un element nou nu va intra în execuție până când &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul care apelează &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; nu iese din zona sincronizată, adică după extragerea unui element de pe stivă&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;A &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;reactivated using &lt;/ins&gt;&amp;#039;&amp;#039;notify&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;needs to wait for the monitor unlocking in order to enter the synchronized section&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;like any other &lt;/ins&gt;thread. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Notice this behavior in method &lt;/ins&gt;&amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;where &lt;/ins&gt;&amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;has been called, before extracting the element from the stack&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;on the previous line&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The program functions correctly because no other &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;will start execution until the current &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;exists the synchronized area&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Resurse externe&lt;/del&gt;:&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;External resources&lt;/ins&gt;:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# http://docs.oracle.com/javase/tutorial/essential/concurrency/&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# http://docs.oracle.com/javase/tutorial/essential/concurrency/&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# http://www.tutorialspoint.com/java/java_multithreading.htm&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# http://www.tutorialspoint.com/java/java_multithreading.htm&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rhobincu</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1613&amp;oldid=prev</id>
		<title>Rhobincu: /* Synchronizing Threads - Semaphore */</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1613&amp;oldid=prev"/>
		<updated>2013-12-23T13:14:50Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Synchronizing Threads - Semaphore&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 23 decembrie 2013 13:14&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l102&quot; &gt;Linia 102:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 102:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Synchronizing Threads - &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Semaphore &lt;/del&gt;==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Synchronizing Threads - &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Semaphores &lt;/ins&gt;==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Există situații&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;în aplicații multithread-ed, în care mai multe thread-uri accesează aceeași resursă&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În unele din aceste situații&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;dacă unele metode sau zone de &lt;/del&gt;program &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ale acestor resurse comune sunt accesate de mai multe thread-uri în același timp&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;pot apărea condiții limită care duc la un comportament incorect al aplicației&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Aceste zone trebuie să fie executate &amp;#039;&amp;#039;atomic&amp;#039;&amp;#039;&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;adică în timpul execuției lor&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;nici un alt &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;nu trebuie să întrerupă &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul curent sau să &lt;/del&gt;execute &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;aceeași bucată de cod&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;There are situations in multithreading applications&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;where multiple threads are accessing a common resource&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In some of these situations&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;if some methods are &lt;/ins&gt;program &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;areas using these common resources are accessed by multiple threads at the same time&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;certain events occur that result in the malfunction of the application&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;These areas need to be executed atomically&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;meaning that during their execution by a thread&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;no other &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;can interrupt the current &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;or &lt;/ins&gt;execute &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;the same area&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În mașina virtuală&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;acest lucru se realizează cu ajutorul unui sistem de monitoare&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Monitorul este un câmp ascuns definit în clasa &lt;/del&gt;Object (&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;astfel încât orice obiect poate fi folosit pe post de &lt;/del&gt;monitor) &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;care contorizează thread-urile care accesează una sau mai multe bucăți de program&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Aceste bucăți de program se numesc &lt;/del&gt;&amp;#039;&amp;#039;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;sincronizate&lt;/del&gt;&amp;#039;&amp;#039;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Dacă un &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;urmează să intre într-o zonă sincronizată&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;atunci mașina virtuală verifică dacă monitorul e liber&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;adică dacă nici un alt &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;nu execută instrucțiuni din vreo zonă sincronizată de acel &lt;/del&gt;monitor. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Dacă monitorul e ocupat &lt;/del&gt;(&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;adică dacă alt &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;îl deține&lt;/del&gt;), &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;arunci &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul curent se oprește, așteptând eliberarea lui&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Dacă este liber&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;atunci &lt;/del&gt;thread&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-ul curent îl ocupă, începând execuția codului&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In the virtual machine&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;this is achieved with a system of monitors&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;A monitor is a hidden field defined in class &lt;/ins&gt;Object (&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;so that by inheriting it, any object can be used as a &lt;/ins&gt;monitor) &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;which keeps track of the threads that access one or more code sections&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;These sections are called &lt;/ins&gt;&amp;#039;&amp;#039;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;synchronized&lt;/ins&gt;&amp;#039;&amp;#039;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;If a &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is about to enter a synchronized section&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;then the virtual machine verifies if the monitor is not locked&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;meaning that no other &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;executes instructions in any section synchronized by that &lt;/ins&gt;monitor. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The the monitor is locked &lt;/ins&gt;(&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;if another &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;has it locked&lt;/ins&gt;), &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;then the current &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;will stop and wait for its release&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;If the monitor is unlocked&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;the &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;will lock it and it will continue execution&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În &lt;/del&gt;Java, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;cuvântul cheie pentru sincronizarea unei bucăți de cod este &lt;/del&gt;&amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In &lt;/ins&gt;Java, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;the keyword for synchronizing a code section is &lt;/ins&gt;&amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l159&quot; &gt;Linia 159:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 159:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;În exemplul de mai sus&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;dacă două thread-uri apelează în același timp metodele &lt;/del&gt;&amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;și &lt;/del&gt;&amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;atunci, dacă acestea nu ar fi sincronizate, ar putea apărea situații în care contorul &lt;/del&gt;&amp;#039;&amp;#039;stackTop&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ar putea fi incrementat de un &lt;/del&gt;thread, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;apoi decrementat de al doilea înainte ca primul să efectueze scrierea în vectorul &lt;/del&gt;&amp;#039;&amp;#039;stack&amp;#039;&amp;#039;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Dar folosind cuvântul cheie &lt;/del&gt;&amp;#039;&amp;#039;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;syncronized&lt;/del&gt;&amp;#039;&amp;#039;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ne-am asigurat că nici una din metodele declarate astfel și nici una din bucățile de program sincronizate nu vor fi executate în același timp de thread-uri diferite&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;In the example above&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;if two threads call methods &lt;/ins&gt;&amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;and &lt;/ins&gt;&amp;#039;&amp;#039;pop()&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;at the same time and they would not be synchronized&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;situations might occur when the counter &lt;/ins&gt;&amp;#039;&amp;#039;stackTop&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;would be incremented by one &lt;/ins&gt;thread, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;then decremented by the second before the first finishes writing in the array &lt;/ins&gt;&amp;#039;&amp;#039;stack&amp;#039;&amp;#039;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, effectively writing in the wrong position&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;By using keyword &lt;/ins&gt;&amp;#039;&amp;#039;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;synchronized&lt;/ins&gt;&amp;#039;&amp;#039;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;we are safe to assume that none of the methods declared as such and none of the synchronized sections will be executed at the same time by different threads&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Regulă&lt;/del&gt;:&amp;lt;/font&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;O clasă nu poate executa o porțiune de program sincronizată al cărei monitor aparține altui &lt;/del&gt;thread &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;DAR poate executa o metodă sincronizată de un monitor care îi aparține&lt;/del&gt;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Ca exemplu&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;zona sincronizată din metoda &lt;/del&gt;&amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;apelează metoda &lt;/del&gt;&amp;#039;&amp;#039;full()&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;care este la rândul ei sincronizată&lt;/del&gt;.&amp;lt;/div&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Rule&lt;/ins&gt;:&amp;lt;/font&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;A thread can not execute a synchronized section belonging to another &lt;/ins&gt;thread &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;BUT it can execute a synchronized section that already belongs to it&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;As an example&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;the synchronized section in method &lt;/ins&gt;&amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is calling method &lt;/ins&gt;&amp;#039;&amp;#039;full()&amp;#039;&amp;#039;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, which is itself synchronized by the same monitor&lt;/ins&gt;.&amp;lt;/div&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Dacă o metodă este declarată &lt;/del&gt;&amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;acest lucru este echivalent cu &lt;/del&gt;a &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;declara tot conținutul metodei într-un bloc &lt;/del&gt;&amp;#039;&amp;#039;synchronized(&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;this&lt;/del&gt;)&amp;#039;&amp;#039;. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Altfel spus&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;o metodă declarată &lt;/del&gt;&amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;este automat sincronizată folosind ca &lt;/del&gt;monitor &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;obiectul curent&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;If a method is declared synchronized, this is equivalent of declaring the whole method content in a &amp;#039;&amp;#039;synchronized(this)&amp;#039;&amp;#039; block. Therefore, a method declared &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; is automatically synchronized using the current object as a monitor.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;If a static method is declared &lt;/ins&gt;&amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;this is equivalent as declaring the whole method content in &lt;/ins&gt;a &amp;#039;&amp;#039;synchronized(&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;class_name&amp;gt;.class&lt;/ins&gt;)&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;block&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Therefore&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;a static method declared &lt;/ins&gt;&amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is automatically synchronized using the object of type Class associated with the current class as a &lt;/ins&gt;monitor.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Dacă o metodă statică este declarată &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;, acest lucru este echivalent cu a declara tot conținutul metodei într-un bloc &amp;#039;&amp;#039;synchronized(&amp;lt;nume_clasa&amp;gt;.class)&amp;#039;&amp;#039;. Altfel spus, o metodă declarată &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; este automat sincronizată folosind ca monitor obiectul de tip &amp;#039;&amp;#039;&amp;#039;Class&amp;#039;&amp;#039;&amp;#039; asociat clasei:&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;public class StaticSync{&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;public class StaticSync{&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rhobincu</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1612&amp;oldid=prev</id>
		<title>Rhobincu la 23 decembrie 2013 12:55</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1612&amp;oldid=prev"/>
		<updated>2013-12-23T12:55:58Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 23 decembrie 2013 12:55&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Linia 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Java language natively supports &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;(&lt;/del&gt;[http://en.wikipedia.org/wiki/Thread_(computing) threads]&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;)&lt;/del&gt;, which are several sequences of instructions running in parallel, but belonging to the same application. An example would be a server which accepts and manages several connections at the same time. There are two ways of implementing a thread:&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Java language natively supports [http://en.wikipedia.org/wiki/Thread_(computing) threads], which are several sequences of instructions running in parallel, but belonging to the same application. An example would be a server which accepts and manages several connections at the same time. There are two ways of implementing a thread:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* by extending class [http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html java.lang.Thread];&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* by extending class [http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html java.lang.Thread];&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* by implementing interface [http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html java.lang.Runnable].&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* by implementing interface [http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html java.lang.Runnable].&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rhobincu</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1611&amp;oldid=prev</id>
		<title>Rhobincu: Pagină nouă: Java language natively supports ([http://en.wikipedia.org/wiki/Thread_(computing) threads]), which are several sequences of instructions running in parallel, but belonging to the same...</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Concurrent_Programming_-_Threads&amp;diff=1611&amp;oldid=prev"/>
		<updated>2013-12-23T12:55:43Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: Java language natively supports ([http://en.wikipedia.org/wiki/Thread_(computing) threads]), which are several sequences of instructions running in parallel, but belonging to the same...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Java language natively supports ([http://en.wikipedia.org/wiki/Thread_(computing) threads]), which are several sequences of instructions running in parallel, but belonging to the same application. An example would be a server which accepts and manages several connections at the same time. There are two ways of implementing a thread:&lt;br /&gt;
* by extending class [http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html java.lang.Thread];&lt;br /&gt;
* by implementing interface [http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html java.lang.Runnable].&lt;br /&gt;
&lt;br /&gt;
Class &amp;#039;&amp;#039;&amp;#039;Thread&amp;#039;&amp;#039;&amp;#039; as well as interface &amp;#039;&amp;#039;&amp;#039;Runnable&amp;#039;&amp;#039;&amp;#039; define a method called &amp;#039;&amp;#039;run()&amp;#039;&amp;#039;. This method is the start point of the new thread, just like &amp;#039;&amp;#039;public static void main(String[])&amp;#039;&amp;#039; for the main thread. This method &amp;#039;&amp;#039;run()&amp;#039;&amp;#039; can be called in two ways:&lt;br /&gt;
* directly calling it, in which case it is ran on the same thread, as normal method;&lt;br /&gt;
* calling method &amp;#039;&amp;#039;start()&amp;#039;&amp;#039;, which starts a new thread and starts executing method &amp;#039;&amp;#039;run()&amp;#039;&amp;#039; in parallel.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class PrintThread extends Thread{&lt;br /&gt;
&lt;br /&gt;
    private int index;&lt;br /&gt;
&lt;br /&gt;
public PrintThread(int _index){&lt;br /&gt;
    index = _index;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void run(){&lt;br /&gt;
    for(int i=0; i&amp;lt;5; i++){&lt;br /&gt;
        System.out.println(&amp;quot;This is thread &amp;quot; + index);&lt;br /&gt;
        try{&lt;br /&gt;
            //pause for 0.5 seconds (500 ms)&lt;br /&gt;
            Thread.sleep(500);&lt;br /&gt;
        }catch(InterruptedException _ie){&lt;br /&gt;
            System.out.println(_ie.getMessage());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the normal call of method &amp;#039;&amp;#039;run()&amp;#039;&amp;#039;. The program will first display the test on thread 1 five times, then the one on thread 2 five times, then the one on thread 3, etc:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class NormalStarter{&lt;br /&gt;
&lt;br /&gt;
public static void main(String[] _args){&lt;br /&gt;
    for(int i=0; i&amp;lt;5; i++){&lt;br /&gt;
        PrintThread _thread = new PrintThread(i + 1);&lt;br /&gt;
        _thread.run();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the behavior when calling &amp;#039;&amp;#039;start()&amp;#039;&amp;#039;. The program will display texts from all threads at the same time:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class ThreadStarter{&lt;br /&gt;
&lt;br /&gt;
public static void main(String[] _args){&lt;br /&gt;
    for(int i=0; i&amp;lt;5; i++){&lt;br /&gt;
        PrintThread _thread = new PrintThread(i + 1);&lt;br /&gt;
        _thread.start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Same thing by using the &amp;#039;&amp;#039;&amp;#039;Runnable&amp;#039;&amp;#039;&amp;#039; interface:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class PrintRunnable implements Runnable{&lt;br /&gt;
&lt;br /&gt;
    private int index;&lt;br /&gt;
&lt;br /&gt;
public PrintRunnable(int _index){&lt;br /&gt;
    index = _index;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void run(){&lt;br /&gt;
    for(int i=0; i&amp;lt;10; i++){&lt;br /&gt;
        System.out.println(&amp;quot;This is thread &amp;quot; + index);&lt;br /&gt;
        try{&lt;br /&gt;
            //pause for 1 second (1000 ms)&lt;br /&gt;
            Thread.sleep(1000);&lt;br /&gt;
        }catch(InterruptedException _ie){&lt;br /&gt;
            System.out.println(_ie.getMessage());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class ThreadStarterRunnable{&lt;br /&gt;
&lt;br /&gt;
public static void main(String[] _args){&lt;br /&gt;
    for(int i=0; i&amp;lt;5; i++){&lt;br /&gt;
        // polymorphism: PrintRunnable is a Runnable&lt;br /&gt;
        Runnable _runnable = new PrintRunnable(i + 1);&lt;br /&gt;
&lt;br /&gt;
        PrintThread _thread = new Thread(_runnable);&lt;br /&gt;
        _thread.start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Synchronizing Threads - Semaphore ==&lt;br /&gt;
&lt;br /&gt;
Există situații, în aplicații multithread-ed, în care mai multe thread-uri accesează aceeași resursă. În unele din aceste situații, dacă unele metode sau zone de program ale acestor resurse comune sunt accesate de mai multe thread-uri în același timp, pot apărea condiții limită care duc la un comportament incorect al aplicației. Aceste zone trebuie să fie executate &amp;#039;&amp;#039;atomic&amp;#039;&amp;#039;, adică în timpul execuției lor, nici un alt thread nu trebuie să întrerupă thread-ul curent sau să execute aceeași bucată de cod.&lt;br /&gt;
&lt;br /&gt;
În mașina virtuală, acest lucru se realizează cu ajutorul unui sistem de monitoare. Monitorul este un câmp ascuns definit în clasa Object (astfel încât orice obiect poate fi folosit pe post de monitor) care contorizează thread-urile care accesează una sau mai multe bucăți de program. Aceste bucăți de program se numesc &amp;#039;&amp;#039;sincronizate&amp;#039;&amp;#039;. Dacă un thread urmează să intre într-o zonă sincronizată, atunci mașina virtuală verifică dacă monitorul e liber, adică dacă nici un alt thread nu execută instrucțiuni din vreo zonă sincronizată de acel monitor. Dacă monitorul e ocupat (adică dacă alt thread îl deține), arunci thread-ul curent se oprește, așteptând eliberarea lui. Dacă este liber, atunci thread-ul curent îl ocupă, începând execuția codului.&lt;br /&gt;
&lt;br /&gt;
În Java, cuvântul cheie pentru sincronizarea unei bucăți de cod este &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;br /&gt;
public class Stack{&lt;br /&gt;
&lt;br /&gt;
    public static final int MAX_STACK_SIZE = 128;&lt;br /&gt;
    &lt;br /&gt;
    private Object[] stack;&lt;br /&gt;
    private int stackTop;&lt;br /&gt;
&lt;br /&gt;
public Stack(){&lt;br /&gt;
    this(MAX_STACK_SIZE);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public Stack(int _maxSize){&lt;br /&gt;
    stack = new Object[_maxSize];&lt;br /&gt;
    stackTop = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public synchronized boolean empty(){&lt;br /&gt;
    return stackTop == 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public synchronized boolean full(){&lt;br /&gt;
    return stackTop == stack.length;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public Object pop() throws Exception{&lt;br /&gt;
    synchronized(this){&lt;br /&gt;
        if(!empty()){&lt;br /&gt;
            return stack[--stackTop];&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    throw new Exception(&amp;quot;Stack empty&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void push(Object _obj) throws Exception{&lt;br /&gt;
    synchronized(this){&lt;br /&gt;
        if(!full()){&lt;br /&gt;
            stack[stackTop++] = _obj;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    throw new Exception(&amp;quot;Stack full&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
În exemplul de mai sus, dacă două thread-uri apelează în același timp metodele &amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; și &amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;, atunci, dacă acestea nu ar fi sincronizate, ar putea apărea situații în care contorul &amp;#039;&amp;#039;stackTop&amp;#039;&amp;#039; ar putea fi incrementat de un thread, apoi decrementat de al doilea înainte ca primul să efectueze scrierea în vectorul &amp;#039;&amp;#039;stack&amp;#039;&amp;#039;. Dar folosind cuvântul cheie &amp;#039;&amp;#039;syncronized&amp;#039;&amp;#039;, ne-am asigurat că nici una din metodele declarate astfel și nici una din bucățile de program sincronizate nu vor fi executate în același timp de thread-uri diferite.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;Regulă:&amp;lt;/font&amp;gt; O clasă nu poate executa o porțiune de program sincronizată al cărei monitor aparține altui thread DAR poate executa o metodă sincronizată de un monitor care îi aparține. Ca exemplu, zona sincronizată din metoda &amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; apelează metoda &amp;#039;&amp;#039;full()&amp;#039;&amp;#039; care este la rândul ei sincronizată.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Dacă o metodă este declarată &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;, acest lucru este echivalent cu a declara tot conținutul metodei într-un bloc &amp;#039;&amp;#039;synchronized(this)&amp;#039;&amp;#039;. Altfel spus, o metodă declarată &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; este automat sincronizată folosind ca monitor obiectul curent.&lt;br /&gt;
&lt;br /&gt;
Dacă o metodă statică este declarată &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039;, acest lucru este echivalent cu a declara tot conținutul metodei într-un bloc &amp;#039;&amp;#039;synchronized(&amp;lt;nume_clasa&amp;gt;.class)&amp;#039;&amp;#039;. Altfel spus, o metodă declarată &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; este automat sincronizată folosind ca monitor obiectul de tip &amp;#039;&amp;#039;&amp;#039;Class&amp;#039;&amp;#039;&amp;#039; asociat clasei:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;br /&gt;
public class StaticSync{&lt;br /&gt;
&lt;br /&gt;
public static synchronized void printSmth(){&lt;br /&gt;
    System.out.println(&amp;quot;Smth&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void printSmthElse(){&lt;br /&gt;
    synchronized(StaticSync.class){&lt;br /&gt;
        System.out.println(&amp;quot;SmthElse&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;#039;&amp;#039;Race conditions&amp;#039;&amp;#039; - bariera ==&lt;br /&gt;
&lt;br /&gt;
În exemplul anterior, dacă considerăm două thread-uri, unul care pune elemente pe stivă și unul care le consumă, observăm că dacă oricare din thread-uri este mai rapid decât celălalt, se ajunge în situația în care se aruncă o excepție, ori pentru că stiva e plină, ori pentru că s-a golit. Aici apare ceea ce se numește &amp;#039;&amp;#039;race condition&amp;#039;&amp;#039;, adică există una sau două zone de program executate de două thread-uri diferite și în care unul din thread-uri trebuie să ajungă înaintea celuilalt ca programul să se desfășoare corect. În exemplul anterior, dacă stiva este goală, atunci thread-ul care pune un element pe stivă trebuie să ajungă la metoda &amp;#039;&amp;#039;push(Object)&amp;#039;&amp;#039; înainte ca celălalt thread să apeleze metoda &amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;, în caz contrar generându-se o excepție. &lt;br /&gt;
&lt;br /&gt;
Această problemă se rezolvă folosind un sistem de bariere, adică un sistem care oprește unul din thread-uri într-un anumit loc până când o condișie este îndeplinită (de cele mai multe ori, când alt thread ajunge în locul potrivit). În Java, acest lucru se realizează cu ajutorul metodelor &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long)&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long, int)&amp;#039;&amp;#039; și &amp;#039;&amp;#039;notify()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Metodele &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long)&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;wait(long, int)&amp;#039;&amp;#039; blochează thread-ul curent până când un alt thread apelează una din metodele &amp;#039;&amp;#039;notify()&amp;#039;&amp;#039;/ &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039;, sau până când timpul dat ca argument a expirat. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;lt;font color=&amp;quot;#ff0000&amp;quot;&amp;gt;Regulă:&amp;lt;/font&amp;gt; Apelurile metodelor de tip &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039; se fac obligatoriu într-un bloc &amp;#039;&amp;#039;synchronized&amp;#039;&amp;#039; și se folosește ca referință obiectul monitor:&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot;&amp;gt;&lt;br /&gt;
public class Stack{&lt;br /&gt;
&lt;br /&gt;
    public static final int MAX_STACK_SIZE = 128;&lt;br /&gt;
    &lt;br /&gt;
    private Object[] stack;&lt;br /&gt;
    private int stackTop;&lt;br /&gt;
&lt;br /&gt;
public Stack(){&lt;br /&gt;
    this(MAX_STACK_SIZE);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public Stack(int _maxSize){&lt;br /&gt;
    stack = new Object[_maxSize];&lt;br /&gt;
    stackTop = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public synchronized boolean empty(){&lt;br /&gt;
    return stackTop == 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public synchronized boolean full(){&lt;br /&gt;
    return stackTop == stack.length;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public Object pop(){&lt;br /&gt;
    synchronized(this){&lt;br /&gt;
        while(empty()){&lt;br /&gt;
            try{&lt;br /&gt;
                this.wait();&lt;br /&gt;
            }catch(InterruptedException _ie){&lt;br /&gt;
                  System.out.println(&amp;quot;InterruptedException: &amp;quot; + _ie.getMessage());&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        this.notifyAll();&lt;br /&gt;
        return stack[--stackTop];&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void push(Object _obj){&lt;br /&gt;
    synchronized(this){&lt;br /&gt;
        while(full()){&lt;br /&gt;
            try{&lt;br /&gt;
                this.wait();&lt;br /&gt;
            }catch(InterruptedException _ie){&lt;br /&gt;
                  System.out.println(&amp;quot;InterruptedException: &amp;quot; + _ie.getMessage());&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        stack[stackTop++] = _obj;&lt;br /&gt;
        this.notifyAll();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Metodele de tip &amp;#039;&amp;#039;notify()&amp;#039;&amp;#039; reactivează threadurile oprite folosind metodele &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039;. Acestea nu trebuie obligatoriu să fie apelate dintr-un bloc sincronizat, dar este recomandat. Se folosește ca referință tot obiectul de tip monitor utilizat pentru apelul metodelor &amp;#039;&amp;#039;wait()&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Un thread reactivat folosind &amp;#039;&amp;#039;notify()&amp;#039;&amp;#039; trebuie să aștepte eliberarea monitorului pentru a executa zona sincronizată, ca orice alt thread. Observați utilizarea acestei funcționalități în metoda &amp;#039;&amp;#039;pop()&amp;#039;&amp;#039;, unde s-a apelat &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; înainte de extragerea elementului de pe stivă, pe linia următoare. Totuși, clasa funcționează corect, pentru că thread-ul care introduce pe stivă un element nou nu va intra în execuție până când thread-ul care apelează &amp;#039;&amp;#039;notifyAll()&amp;#039;&amp;#039; nu iese din zona sincronizată, adică după extragerea unui element de pe stivă.&lt;br /&gt;
&lt;br /&gt;
Resurse externe:&lt;br /&gt;
# http://docs.oracle.com/javase/tutorial/essential/concurrency/&lt;br /&gt;
# http://www.tutorialspoint.com/java/java_multithreading.htm&lt;/div&gt;</summary>
		<author><name>Rhobincu</name></author>
	</entry>
</feed>