0

I am currently learning Java, and I have reached the topic of Synchronization.

For some reasons, the code below (based on the code from The Complete Reference JAVA - Herbert Schildt 7th Edition, Pg no. 239-240) does not give the required output.

Code:

package package1;
class Call{
    synchronized public void call(String msg){
        System.out.print("[" + msg);
        try{
            Thread.sleep(100);
        }catch (InterruptedException e){
            System.out.println("Interrupted Exception Caught");
        }
        System.out.println("]");
    }
}

class CallMe implements Runnable{
    String msg;
    Call target;
    Thread t;

    public CallMe(Call targ, String message){
        target = targ;
        msg = message;
        t = new Thread(this);
        t.start();
    }

    public void run(){
        target.call(msg);
    }
}

public class Synchronization {
    public static void main(String[] args) {
        Call target = new Call();
        CallMe obj1 = new CallMe(target, "Hello");
        CallMe obj2 = new CallMe(target, "Synchronized");
        CallMe obj3 = new CallMe(target, "World");

        try{
            obj1.t.join();
            obj2.t.join();
            obj3.t.join();
        }catch (InterruptedException e){
            System.out.println("Interrupted Exception Caught");
        }
    }
}

The Required Output:

[Hello]
[Synchronized]
[World]

Actual Output (I am using Eclipse on Macbook Pro late 2013):

[Hello]
[World]
[Synchronized]

I have read that all these topic's output differ from computer to computer.

Could anyone please explain me why does not this work?

1
  • Yes, there is nothing in this code that would guarantee a specific order. Commented Mar 23, 2014 at 12:47

2 Answers 2

3

Because the different CallMe instances are invoked in parallel and you have no guarantee in which order they are processed. The only thing you know is, that Call.call(String) is not invoked in parallel as this method is synchronized.

Sign up to request clarification or add additional context in comments.

2 Comments

So this there no way to work around this? Because the book got the "required output" ..
@BigFatProgrammer: The naive solution is to not use threads. If you want sequential behaviour, then don't use parallelisation.
0

To piggy-back a little on Peter's answer. All the synchronization does is prevent things from going crazy.

Trying this code without the synchronized call and you can see that the call class can hit .out as it pleases without regard for the other objects. I get an output like this, with "synchronized" removed:

[Hello[World[Synchronized]
]
]

With synchronized in you get something like what you see,.. but it doesn't guarantee order. Sometimes you get:

[Hello]
[World]
[Synchronized] 

Sometimes you get:

[Hello]
[Synchronized]
[World]

Run it over and over again and you will see what I mean.
To really throw it off try using:

Thread.sleep(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.