Run Method

N

ndxp

I'm kind of confused here -

class A extends Thread {

public void run() {

synchronized (...) {

}
}

}

what may be the use of having a synchronized block in a thread's run
method ? I was under
the impression, that only one thread will access the run method i,e the
current running thread.

-t
 
B

BartCr

You can have multiple A's all running at the same time, synchronizing
on a shared object. That way only one of all A threads will execute the
code in the block at the same time.

A use for this might be where multiple threads append to the same file
from that run method. This way it is assured that the written data will
not end mixed up.

Bart
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top