0

I like to use ReentrantLock even when the same result could be achieved with synchronized. I don't really do it for performance but just because I prefer to use the try-catch-finally block of locks.

The question is: is it acceptable to use ReentrantLock in any situation where synchronized could be used or is it bad practice and synchronized should be used over locks?

Spyromancer
  • 435
  • 1
  • 3
  • 10
  • 1
    "I don't really do it for performance or any other specific reason but just for personal preference" I'm not sure I understand how you can have a preference when you don't understand the difference – Michael Sep 04 '21 at 16:18
  • A `ReentrantLock` is much more versatile than `synchronized`. For example, we can call [`tryLock(long timeout, TimeUnit unit)`](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/concurrent/locks/ReentrantLock.html), which is not possible with `synchronized`. Likewise, we can unlock the lock in a method different from the one that locked it. The flexibility, however, comes at a cost: we have to control all this behaviour manually. I would, whenever possible, advice to use `synchronized` since it is an easier to understand construct. – Turing85 Sep 04 '21 at 16:21
  • @Micheal Why are you assuming that I don't know the difference? – Spyromancer Sep 04 '21 at 16:22
  • Because if you knew the difference then your preference would be based on some reasoning, which you stated it wasn't – Michael Sep 04 '21 at 16:23
  • @Micheal I wrote I do it for personal preference, by personal preference I mean that in the situations where the two does the same thing, I prefer using locks over syncrhonized, simply because I like it more. The question was created to understand it this personal preference of mine is actually bad practice or not. – Spyromancer Sep 04 '21 at 16:29
  • @Mizzet I would argue that it is a bad practice because you use something that you do not really need. `synchronized` is easier to understand since it is less flexible than a `ReentrantLock`. Thus, the code is easier to understand. – Turing85 Sep 04 '21 at 16:32
  • @Turing85 I get it, I'll get used to use locks only in situations where synchronized doesn't get the job done. Thanks – Spyromancer Sep 04 '21 at 16:34
  • In a situation where 2 methods "do the same thing" of course it doesn't matter which you choose. But there is no situation where an explicit lock and a monitor lock do precisely the same thing. There are always subtle nuances, so your choice should always be based on those. So to answer your question, choosing programming constructs you "like" for arbitrary reasons is bad practice in my opinion, yes. – Michael Sep 04 '21 at 16:35
  • @Micheal To be more specific the personal preference would be based on the fact that I prefer to use the try-catch-finally block of locks because I think the logic of the code looks more clear that way. Like Turing85 already stated, using Synchronized would be preferred for the reasons he mentioned, so I'll get used to use synchronized. – Spyromancer Sep 04 '21 at 16:43
  • Ok, so when you stated in your question "I don't really do it for ... any other specific reason", that wasn't true. You do it for the specific reason that you think it's clearer. – Michael Sep 04 '21 at 16:45
  • @Micheal Yes, my bad. I should have been more specific. – Spyromancer Sep 04 '21 at 16:46

0 Answers0