Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Thread Synchronization - placement of synchronized keyword
    primarykey
    data
    text
    <p>I am trying to synchronize a thread application using the synchronized keyword. It acts weird at times. There is an ambiguity in placement of the keyword synchronized. The code is as follows :</p> <pre><code>class Athread implements Runnable { public void run() { System.out.println("Starting Implementation of Class Athread"); for(int i=1;i&lt;=10;i++) { System.out.println("Class Athread :"+i); } System.out.println("Ending Implementation of class Athread"); } } class Bthread implements Runnable { public void run() { System.out.println("Starting Implementation of Class Bthread"); for(int i=11;i&lt;=20;i++) { System.out.println("Class Bthread :"+i); } System.out.println("Ending Implementation of class Bthread"); } } public class ThreadDemo { public static void main(String[] args) { System.out.println("Program starts.."); Athread t1 = new Athread(); Thread th1 = new Thread(t1); Bthread t2 = new Bthread(); Thread th2 = new Thread(t2); synchronized(th1){ th1.start(); } synchronized(th2){ th2.start(); } System.out.println("Program ends..."); } } </code></pre> <p>When I place the synchronized block at the start of my thread it should lock that thread and not interleave with other threads. Sadly, the output is not as expected. My output is as follows :</p> <pre><code>Program starts.. Program ends... Starting Implementation of Class Athread Starting Implementation of Class Bthread Class Athread :1 Class Bthread :11 Class Athread :2 Class Bthread :12 Class Athread :3 Class Bthread :13 Class Athread :4 Class Bthread :14 Class Athread :5 Class Bthread :15 Class Athread :6 Class Bthread :16 Class Athread :7 Class Bthread :17 Class Athread :8 Class Bthread :18 Class Athread :9 Class Bthread :19 Class Athread :10 Class Bthread :20 Ending Implementation of class Athread Ending Implementation of class Bthread </code></pre> <p>Even when I use the synchronized keyword in the run() method of both the class I do not get the actual desired output. </p> <p>Can anyone please explain me how synchronized keyword can be applied to my above example.</p> <p>Thanks.</p> <p><strong>UPDATE:</strong></p> <p>In the question above I had wrongly understood the concept of synchronization. According to the comments received I am trying to call <strong><em>two different threads and access two different resources</em></strong>.But synchronization should be done on a shared/common resource. Hence there should be a common location on which I should try synchronizing. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload