Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Ad icon
Video Content Creator
pramukag
Updated:
Yesterday at 6:10 AM
Ad icon
QA Engineer Intern
pramukag
Updated:
Yesterday at 6:07 AM
Ad icon
Sell your Land, House on idamata.lk for FREE
sajith.xp.pk
Updated:
Thursday at 9:03 AM
Handmade Character Soft Toys
anil1961
Updated:
Tuesday at 2:11 PM
Bodim.lk out now !
Manoj Suranga Bandara
Updated:
Jun 21, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Java danuma bedaganna kamathi aya innawada?
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="azam426" data-source="post: 11150565" data-attributes="member: 55428"><p>/*</p><p>FileName: ATEST16.java</p><p>The output of this example is as follows:</p><p> Entered the testcase</p><p> Synchronize to prevent access to shared data</p><p> Create/start the thread</p><p> Thread Thread-1: Entered</p><p> Thread Thread-2: Entered</p><p> Wait a bit until we're 'done' with the shared data</p><p> Thread Thread-3: Entered</p><p> Unlock shared data</p><p> Thread Thread-1: Start critical section, in synchronized block</p><p> Thread Thread-1: End critical section, leave synchronized block</p><p> Thread Thread-2: Start critical section, in synchronized block</p><p> Thread Thread-2: End critical section, leave synchronized block</p><p> Thread Thread-3: Start critical section, in synchronized block</p><p> Thread Thread-3: End critical section, leave synchronized block</p><p> Wait for the threads to complete</p><p> Testcase completed</p><p>*/</p><p>import java.lang.*;</p><p> </p><p> </p><p>public class ATEST16 {</p><p> public final static int NUMTHREADS = 3;</p><p> public static int sharedData = 0;</p><p> public static int sharedData2 = 0;</p><p> /* Any real java object or array would suit for synchronization */</p><p> /* We invent one here since we have two unique data items to synchronize */</p><p> /* An in this simple example, they're not in an object */</p><p> static class theLock extends Object {</p><p> }</p><p> static public theLock lockObject = new theLock();</p><p> </p><p> static class theThread extends Thread {</p><p> public void run() {</p><p> System.out.print("Thread " + getName() + ": Entered\n");</p><p> synchronized (lockObject) {</p><p> /********** Critical Section *******************/</p><p> System.out.print("Thread " + getName() +</p><p> ": Start critical section, in synchronized block\n");</p><p> ++sharedData; --sharedData2;</p><p> System.out.print("Thread " + getName() +</p><p> ": End critical section, leave synchronized block\n");</p><p> /********** Critical Section *******************/</p><p> }</p><p> }</p><p> }</p><p> </p><p> public static void main(String argv[]) {</p><p> theThread threads[] = new theThread[NUMTHREADS];</p><p> System.out.print("Entered the testcase\n");</p><p> </p><p> System.out.print("Synchronize to prevent access to shared data\n");</p><p> synchronized (lockObject) {</p><p> </p><p> System.out.print("Create/start the thread\n");</p><p> for (int i=0; i<NUMTHREADS; ++i) {</p><p> threads<em> = new theThread();</em></p><p><em> threads<em>.start();</em></em></p><p><em><em> }</em></em></p><p> <em><em></em></em></p><p><em><em> System.out.print("Wait a bit until we're 'done' with the shared data\n");</em></em></p><p><em><em> try {</em></em></p><p><em><em> Thread.sleep(3000);</em></em></p><p><em><em> }</em></em></p><p><em><em> catch (InterruptedException e) {</em></em></p><p><em><em> System.out.print("sleep interrupted\n");</em></em></p><p><em><em> }</em></em></p><p><em><em> System.out.print("Unlock shared data\n");</em></em></p><p><em><em> }</em></em></p><p> <em><em></em></em></p><p><em><em> System.out.print("Wait for the threads to complete\n");</em></em></p><p><em><em> for(int i=0; i <NUMTHREADS; ++i) {</em></em></p><p><em><em> threads<em>.join();</em></em></em></p><p><em><em><em> }</em></em></em></p><p><em><em><em> catch (InterruptedException e) {</em></em></em></p><p><em><em><em> System.out.print("Join interrupted\n");</em></em></em></p><p><em><em><em> }</em></em></em></p><p><em><em><em> }</em></em></em></p><p> <em><em><em></em></em></em></p><p><em><em><em> System.out.print("Testcase completed\n");</em></em></em></p><p><em><em><em> System.exit(0);</em></em></em></p><p><em><em><em> }</em></em></em></p><p> <em><em><em></em></em></em></p><p><em><em><em>}</em></em></em></p></blockquote><p></p>
[QUOTE="azam426, post: 11150565, member: 55428"] /* FileName: ATEST16.java The output of this example is as follows: Entered the testcase Synchronize to prevent access to shared data Create/start the thread Thread Thread-1: Entered Thread Thread-2: Entered Wait a bit until we're 'done' with the shared data Thread Thread-3: Entered Unlock shared data Thread Thread-1: Start critical section, in synchronized block Thread Thread-1: End critical section, leave synchronized block Thread Thread-2: Start critical section, in synchronized block Thread Thread-2: End critical section, leave synchronized block Thread Thread-3: Start critical section, in synchronized block Thread Thread-3: End critical section, leave synchronized block Wait for the threads to complete Testcase completed */ import java.lang.*; public class ATEST16 { public final static int NUMTHREADS = 3; public static int sharedData = 0; public static int sharedData2 = 0; /* Any real java object or array would suit for synchronization */ /* We invent one here since we have two unique data items to synchronize */ /* An in this simple example, they're not in an object */ static class theLock extends Object { } static public theLock lockObject = new theLock(); static class theThread extends Thread { public void run() { System.out.print("Thread " + getName() + ": Entered\n"); synchronized (lockObject) { /********** Critical Section *******************/ System.out.print("Thread " + getName() + ": Start critical section, in synchronized block\n"); ++sharedData; --sharedData2; System.out.print("Thread " + getName() + ": End critical section, leave synchronized block\n"); /********** Critical Section *******************/ } } } public static void main(String argv[]) { theThread threads[] = new theThread[NUMTHREADS]; System.out.print("Entered the testcase\n"); System.out.print("Synchronize to prevent access to shared data\n"); synchronized (lockObject) { System.out.print("Create/start the thread\n"); for (int i=0; i<NUMTHREADS; ++i) { threads[i] = new theThread(); threads[i].start(); } System.out.print("Wait a bit until we're 'done' with the shared data\n"); try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.print("sleep interrupted\n"); } System.out.print("Unlock shared data\n"); } System.out.print("Wait for the threads to complete\n"); for(int i=0; i <NUMTHREADS; ++i) { threads[i].join(); } catch (InterruptedException e) { System.out.print("Join interrupted\n"); } } System.out.print("Testcase completed\n"); System.exit(0); } }[/i][/i][/i] [/QUOTE]
Insert quotes…
Verification
Dawasata paya keeyak thibeda?
Post reply
Top
Bottom