Results 1 to 5 of 5
  1. #1
    naurinfarooq is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default synchronization in java threads

    Well i am new to java and i have a problem implementing synchronized block of code in java threads.


    public class btstep extends Thread{
    private static final Object lock = new Object();
    public void run()
    {
    synchronized(lock)
    {

    System.out.println("3 ");
    System.out.println("4 ");

    }

    }}



    public class btevent extends Thread{
    private static final Object locko = new Object();
    public void run()
    {

    synchronized(locko)
    {
    System.out.println("1 ");
    System.out.println("2 ");

    }


    }}

    public class Interleaving {
    public static btevent bt = new btevent();
    public static btstep bs = new btstep();

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    bt.start();
    bs.start();
    // TODO code application logic here
    }}


    The running output of this program is

    1

    3

    2

    4

    which is not what i want. the synchronized block of code is not serial any more its interleaving
    what i want as an output is

    1

    2

    3

    4


    or

    3

    4

    1

    2


    What am i doing wrong in the program??

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: synchronization in java threads

    If you want to prevent interleaving, both threads must synchronize on the same object.
    Get in the habit of using standard Java naming conventions!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: synchronization in java threads

    Both Threads are synchronizing on different objects, i.e. lock and locko; they don't know what the other Thread did; make them synchronize on the same object and you get what you want.

    kind regards,

    Jos

    edit: and who's the slow old sod again?
    Last edited by JosAH; 07-22-2012 at 11:25 AM.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: synchronization in java threads

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    naurinfarooq is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: synchronization in java threads

    Thank you all for the reply

Similar Threads

  1. Multiple Threads and Synchronization
    By yolo in forum Threads and Synchronization
    Replies: 6
    Last Post: 04-28-2012, 11:25 PM
  2. Threads synchronization with wait() & notify() problem
    By Cropcircles in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-25-2012, 11:44 PM
  3. synchronization issue with multiple threads
    By harmeet in forum Threads and Synchronization
    Replies: 0
    Last Post: 05-01-2011, 05:01 AM
  4. Threads and Synchronization
    By ASADUN in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-18-2009, 08:00 AM
  5. Odd Even number synchronization in threads
    By DukeNukem in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-16-2009, 03:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •