0

In Java using the synchronized keyword all within a single object and thread.

Can I call a synchronized method that calls a non-synchronized method that calls a synchronized method, without the final synchronized method blocking for the first synchronized method to be completed?

willjgriff
  • 783
  • 2
  • 6
  • 11
  • try it...but the answer is as far as i know No. – René Winkler Apr 30 '15 at 11:22
  • AFAIK we can, but there are good chances of dead lock – HJK Apr 30 '15 at 11:24
  • Answer is: it depends on how your synchronization is organized and on state of each monitor. If you have single monitor (lock) and only one thread then there shouldn't be a problem with it, since in locks in Java are reentrant (thread can enter in synchronized block on lock it already posses more than once - it will just increase counter of that monitor for that thread). – Pshemo Apr 30 '15 at 11:26

3 Answers3

5

With a single object and a single thread, there's no problem.

Your only thread is able to acquire all the locks, and due to re-entrancy, it can acquire them several times.

Even if we add another thread, it'll work (with one object). One thread will acquire the first lock, blocking the second thread, and execution continues normally.

With multiple threads and multiple objects, the answer is "it depends on how the code is written".

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

Your code can always call a method whether it is synchronized or not. A better question is, what will happen when your code calls it.

A synchronized method that looks like this:

synchronized void foobar() {
    doSomething();
}

Actually is just a short-hand way of writing this:

void foobar() {
    synchronized(this) {
        doSomething();
    }
}

So any question about calling a synchronized method really is a question about executing a synchronized block. There are three things that could happen when your code enters synchronized(this) {...}.

1) If the this object is not locked, then it will be locked in the name of the calling thread, the thread will execute the statements in the block, and then it will unlock the lock when it's done.

2) If the this object already has been locked by the calling thread, then the thread will simply execute the statements in the block.

3) If the this object is locked by some other thread, then the calling thread will wait until the other thread unlocks it, and then it will proceed as in case (1).

The only way you can get into trouble is if your code attempts to lock two different locks. Then, if your design is not well thought out, two or more threads could deadlock, which is something you can read about elsewhere.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

If a class has both synchronized and non-synchronized methods, multiple threads can still access the class's non-synchronized methods.If you have methods that don't access the data you're trying to protect, then you don't need to synchronize them. Synchronization can cause a hit in some cases (or even deadlock if used incorrectly)

Mohan Raj
  • 1,104
  • 9
  • 17