I've read the oracle doc about synchronized methods and how they may introduce a lock to the multithreaded program, but there is one thing that is unclear to me. Are the subsequent calls to an already locked methods queued?
Lets say we have a class:
class Astore {
...
public synchronized void a() {
doSomethingTimeConsuming();
}
...
}
and 3 threads that call astore.a()
final Astore astore = new Astore();
Thread t1 = new Thread(new Runnable() {
public void run() {
astore.a();
doSomethingElse();
astore.a();
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
astore.a();
}
});
t2.start();
Thread t3 = new Thread(new Runnable() {
public void run() {
astore.a();
}
});
t3.start();
I'm not sure if I've made the example correctly, but the point is, that 3 threads make a call to the same object with synchronized method almost at the same time.
Will the order of operations be stored in a queue so that the threads invoking will be:
- t1 (as it was called first)
- t2 (was called after T1)
- t3
- t1 again (it was busy doing something with A already while other threads requested method)
Can I safely assume that will be the behavior, or there is no guarantee that this will be the order (or even worse, t2 and t3 might get called in random order)
What is the best practice when multiple threads may need to share data (for instance a socket server with one thread for each active connection - I don't want 6 clients to time out while waiting for the first one to finish a huge upload to a shared data structure)
What is the best practice when multiple threads may need to share data (for instance a socket server with one thread for each active connection
That is the billion dollar question. Entire libraries can be filled with books on just that one subject. A more specific question is more likely to be answered. – ReliquedoSomethingTimeConsuming()
is a mistake. One of the most important guidelines you can follow is to keep yoursynchronized
blocks as small as possible. The real art of multi-threaded programming is to design your program so that its threads do not waste time waiting for one another when there is work that they could be doing. If your program does any I/O inside asynchronized
block, or if a synchronized block updates more variables than you can count on your fingers, then you might want to re-consider your design. – Counterforcet1
thread will callastore.a()
before either of the other two threads calls it. Each of the threestart()
calls creates a new thread and makes the new thread runnable. But, "runnable" is not the same thing as "running". It is entirely possible for all three of thestart()
calls to complete before any of the new threads actually begins to run, and it is entirely up to the operating system to choose the order in which they will get to run. – Counterforce