Results 1 to 4 of 4
  1. #1
    BigHotCuppa is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Problems with synchronized

    I am flumoxed. I thought I understood how "synchronized" works, but it appears I don't.
    This is my main program:

    XML Code:
    public class MainClass {
      private final static int NUMTESTERS=6;
      public MainClass(){
        Tester[] testers = new Tester[NUMTESTERS];
        for(int i=0;i<testers.length;i++){
          testers[i]=new Tester(i);
        }
        for(int i=0;i<testers.length;i++){
          testers[i].start();
        }
      }
      public static void main(String[] args){
        new MainClass();
      }
    }

    Basically, it creates an array of "Tester" objects (which inherit from Thread),
    then calls start on each one. Each one has an identity number.
    Tester looks like this:
    XML Code:
    class Tester extends Thread{
      private int id;
      private final static int NUMTHREADS=20;
      public Tester(int idval){
        id=idval;
      }
      public void run(){
        for(int i=0;i<NUMTHREADS;i++){
          synchro();
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            //ignore
          }
        }
      }
      public synchronized void synchro(){
        System.out.print("<"+id+" ");
        //busy loop
        int k=1;
        for (int i=0;i<1000+(int)(Math.random()*1000);i++){
          k+=k/k;
        }    
        System.out.println(id+">");
      }
    }

    The run function calls "synchro" repeatedly, and being synchronized, should only let one thread access it at a time.
    That begin the case, I would expect the output to look something like this:
    <0 0><4 4><1 1><6 6>
    and so on, as random threads enter synchro, do their business, and leave.

    But what I actually get is this:
    <1 <4 <5 1> 4> 5><3 <2 3> 2><0 0><1 <5 <4 5> 4> 1>
    and so on. So, immediately after thread 1 starts executing synchro, 4 jumps in, then 5, then 1 finishes, then 4 finishes, then 5 finishes, then 3 enters, then 2 jumps in, then 3 finishes, etc.

    Since synchro is supposed to be synchronized, how can this be?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Doesn't each Tester object have it's own synchro method since it's an instance method of the Tester class? So there should be no locking occurring. Or am I missing something?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, that's the problem.

  4. #4
    BigHotCuppa is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks a lot! I just needed to make the synchronized static.

Similar Threads

  1. Replies: 1
    Last Post: 09-08-2010, 04:08 PM
  2. help regarding synchronized threads
    By vishalrimt in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-08-2010, 12:47 PM
  3. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 01:20 PM
  4. Synchronized Servlet
    By udayadas in forum Java Servlet
    Replies: 5
    Last Post: 09-09-2008, 08:14 PM
  5. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 10:33 AM

Posting Permissions

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