How does Java sync work?

Asked

Viewed 186 times

1

What’s the advantage of having two threads, which run at the same time but which one waits for the other to be completed to be executed? When using the SYNCHRONIZED keyword in a JAVA application with THREADS, it is established that one thread can only be executed after the end of the other. In this case, what is the advantage of using the thread instead of a single-task application?

  • https://answall.com/questions/95233/o-que-%C3%A9-a-thread-how-it-works, I believe this question has the answer you want

2 answers

2


In fact if it happens I shouldn’t use it this way.

People have the belief that putting new threads everything gets fast, but often gets slower. See It’s always guaranteed that a multi-threaded application runs faster than using a single thread?.

If the thread to be stuck waiting for another most of the time then there will only be waste of time, never gain. If this wait is possible may still have some gain, even if it is not so great, so you have to think carefully if it is worth the complexity of doing this, because environment multithreaded is harder to program and a lot can go wrong.

There’s only this problem if at least two threads compete for the same resource during processing. No threads in this scenario the competition will not exist but the speed would be limited. When you put another thread can decrease well the time to finish while they are not competing for the feature and the wait only happens in a few moments, if it happens in fact. In many cases the locking of a thread is very short so the wait is not long. It does not mean that this is good, but if it is eventual is not a big problem.

Where real competition is expected in many cases not worth using this type of strategy. There are data structures that do not need synchronization and allow better competition. Of course they have other disadvantages, so it cannot be used in any case. And most situations the solution is to have a same serialized processing.

Think of a database. Many work like this, it crashes the record that is working because it is unlikely that another operation is accessing the same data, but if this happens then there will be a wait and this is not good, but it is better that this operation access the data that is already being accessed. Some databases use a system called MVCC in which everything changed is copied so access does not create problems.

If the accesses will be read-only the synchronization is not necessary.

1

I do not understand how duplicated because the question is more specific than "what is thread", well the answer is relatively simple, this situation you presented the biggest reason is that "if you pay for slower processing"

Say

A thread execution where the first one contains a code that takes on average 1 second to execute and in the second thread contains a code that takes 3 seconds, if the processes were executed sequentially the delay would be a total of 4 seconds, but running in parallel the fastest execution pays the price of the slowest so the values do not add up and only the slowest is taken into account.

Obviously there is a loss of performance in breaking threads should be observed case by case the need, but basically the logic is this, to pay only for slower processing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.