synchronized confusion!

S

shoe

hi all!
If a method is synchronized like this:
public synchronized void broadcast (String message) {

I have like 10 threads calling the broadcast method.
What happens to all threads calling this method?
Does the threads halt/wait and line up in a qeueue??
Is it like when one use the Wait-notify system or what.
Does the threads stop what they are doing when they
try to get access to the synchronized method and the method
is locked.

I know that when I use the wait() it will put
thread's in the Wait mode. I can then do a notify/all
and the waiting threds will continue. This is easy to
understand. But the synchronized is not like that I guess

Hope you understand what I mean!
 
S

Steve Horsley

shoe said:
hi all!
If a method is synchronized like this:
public synchronized void broadcast (String message) {

I have like 10 threads calling the broadcast method.
What happens to all threads calling this method?
Does the threads halt/wait and line up in a qeueue??
Is it like when one use the Wait-notify system or what.
Does the threads stop what they are doing when they
try to get access to the synchronized method and the method
is locked.

I know that when I use the wait() it will put
thread's in the Wait mode. I can then do a notify/all
and the waiting threds will continue. This is easy to
understand. But the synchronized is not like that I guess

Hope you understand what I mean!


With a synchronized method (or code block), only one thread can
be inside it at a time. While one thread is inside, any other
thread trying to enter will block until until the first thread
has left. The waiting threads block as though waiting in a queue,
although it is not an ordered queue - there is no guarantee that
the first to start waiting will be the next to get through the
gate. It is as though the blocked threads all enter a wait state
and when the current thread leaves the method a notify() allows
one through.

The best analogy I can think of is a crowd of people all waiting
outside the bathroom at a party, and one at a time gets to go in,
do their thing and then leave again.

Steve
 
L

Lasse Reichstein Nielsen

public synchronized void broadcast (String message) {

I have like 10 threads calling the broadcast method.
What happens to all threads calling this method?

They are guaranteed mutual exclusion.
Does the threads halt/wait and line up in a qeueue??

They try to get the lock on the object containing the
method. They are blocked if another thread currently has
the lock. When the lock is released, one of the blocked
threads will continue and get the lock.

There is no guarantee that the access is queue-like (FIFO).
Is it like when one use the Wait-notify system or what.
Does the threads stop what they are doing when they
try to get access to the synchronized method and the method
is locked.

Yes. Waiting on a lock is one of the states of a Java Thread.
I know that when I use the wait() it will put
thread's in the Wait mode. I can then do a notify/all
and the waiting threds will continue. This is easy to
understand. But the synchronized is not like that I guess

Simpler, actually. It's an airlock. At most one thread can
be inside the airlock at a time, and the rest have to wait
outside until there is room.

/L
 
G

Gordon Beaton

If a method is synchronized like this:
public synchronized void broadcast (String message) {

I'd just like to add a small but important detail to the answers
you've already got.

Based on your short code example, threads invoking a synchronized
method on a *given* object will be mutually synchronised; threads
holding other instances of this object will *not*.

If you want to synchronize this method regardless of what object it is
called on, you need to specify a common object for them to synchronize
on, something like this:

public void broadcast(String message) {
synchronized (someObject) {
[ body of method here ]
}
}

/gordon
 
S

shoe

Thanks all for the clarification.

I have created a chat server/client using socket.
There are some dead look maybe if ALL Clients
are sending a chat message at the same time like this:

(All chat clients connected to the server are using this method
to send there message to every other client)

protected synchronized void broadcast (String message) {

Enumeration e = vectorBuddys.elements ();
while (e.hasMoreElements ()) {
ClientHandlerThread c = (ClientHandlerThread) e.nextElement ();
try {
c.theOutputStream.writeUTF (message);

c.theOutputStream.flush ();
} catch (IOException ex) {
}
}
}


Clearly this makes threads wait in line to be able to
give there message to the server. There can be another
message coming in through the DataInputStream socket
on all Client. But what happens if the Client is in
synchronized queue waiting to deliver the first message?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top