Java, why need to use synchronization? instead of using a single thread?

While reading about Java synchronized, I just wondered, if the processing should be in synchronization, why not just creating a single thread (not main thread) and process one by one instead of creating multiple threads.

Because, by ‘synchronized’, all other threads will be just waiting except single running thread. It seems like the only single thread is working in the time.

Please advise me what I’m missing it.

I would very appreciate it if you could give some use cases.

I read an example, that example about accessing bank account from 2 ATM devices. but it makes me more confused, the blocking(Lock) should be done by the Database side, I think. and I think the ‘synchronized’ would not work in between multiple EC2 instances.

If my thinking is wrong, please fix me.

Let’s consider the following use-case :

Your application is a internet browser game. Every player has a score and can click a button. Every time a player clicks the button, their score is increased and their opponent’s is decreased. The first player to reach 10 wins.

As per the nature of the game, and to single a unique winner, you have to consider the two counters increase (and the check for the winner) atomically.

You’ll have each player send clickEvents on their own thread and every event will be translated into the increase of the owner’s counter, the check on whether the counter reached 10 and the decrease of the opponent’s counter.

This is very easily done by synchronizing the method which handles modifying the counters : every concurrent thread will try to obtain the lock, and when they do, they’ll execute the code (and finally release the lock).

The locking mechanism is pretty lightweight and only requires a single keyword of code.

If we follow your suggestion to implement another thread that will handle the execution, we’d have to implement the whole thread management logic (more code), to initialize that Thread (more resource) and even so, to guarantee fairness in the handling of events, you still need a way for your client threads to pass the event to your executor thread. The only way I see to do so, is to implement a BlockingQueue, which is also synchronized to prevent the race condition that naturally occurs when trying to add elements from two other thread.

I honnestly don’t see a way to resolve this very simple use-case without synchronization (or implementing your own locking algorithm that basically does the same).

If all the code you run with several threads is within a synchronized block, then indeed it makes no difference vs. using a single thread.

However in general your code contains parts which can be run on several threads in parallel and parts which can’t. The latter need synchronization but not the former. By using several threads you can speed up the “parallelisable” bits.

You can have a single thread and process one-by-one (and this is done), but there are considerable overheads in doing so and it does not remove the need for synchronization.

You are in a situation where you are starting with multiple threads (for example, you have lots of simultaneous web sessions). You want to do a part of the processing in a single thread – let’s say updating some common structure with some new data. You need to pass the new data to the single thread – how do you get it there? You would have to use some kind of message queue (or an equivalent thing) and have the single thread pick requests off the message queue and that would have have to be synchronized anyway, plus there is the overhead of managing the queue, plus the issue that you need to get a reply back from the single thread asynchronously. So you are back to square one.

This technique is used where the processing you need to do is considerable and you don’t want to block your main threads for a long time.

In summary: having a single thread does not remove the need for synchronization.