How to chose when to synchronize an object or a method?

I found this synchronization example on the Internet and I don't really understand what is the difference between synchronizating the object and the methods in this particular example. Here the synchronization is in the object sender, would it be possible to synchronize the method send and obtain the same result?

Following is an example of multi threading with synchronized.

// A Java program to demonstrate working of

// synchronized.


import java.util.*;

// A Class used to send a message

class Sender


public void send(String msg)


System.out.println("Sending\t" + msg...

Read More »

By: StackOverFlow - Friday, 9 November

Related Posts