简体   繁体   中英

How to work with multiple threads in a most effective and without synchronize in java?

There is a bus booking site and around 100k threads are acting on it but total seats are only 20. How will you control the performance and what will be your approach in Java multithreading?

I replied that i will go for synchronize methods or block as it will control the concurrent threads and lock will prevent asynchronous execution.

But the interviewer interrupted me and said that syncronize is a bogus idea and performance will degrade and its not helpful.

Please let me know if i am missing any other useful multithreading concept that can fix this issue

You have to use Semaphore of permits 20, it means at a time 20 passengers can occupy seats if passengers are considered as thread. This may be answer only in case of core java multi threading.

This question is not about thread concept. Interviewer actually wants you not to use locking because that will make the application very slow if 100K threads are accessing the same object. It can be achieved with the optimistic locking (in jpa or hibernate).

This is accomplished by a version column in the database table, with a corresponding version attribute (@Version annotation) in the entity class. When a row is modified, the version value is incremented. The original transaction checks the version attribute, and if the data has been modified by another transaction, a javax.persistence.OptimisticLockException will be thrown, and the original transaction will be rolled back. We can use Date and timeStamp as well.

Please have a look here javaEE Entity locking docs.oracle.com

An interviewer who asks a question like this is not looking for some specific solution or idea. They're looking to hear how you reason about synchronization. A sensible answer might have been:

With so many threads, clearly nobody cares about performance. If you care about performance, reduce the number of threads. They're just fighting over the same resource.

But if you must have so many threads, using locks makes a lot of sense as it will negate some of the harm from having so many threads by keeping most of the threads out of the ready-to-run state for as long as possible. While some method other than locks might keep more threads running at the same time, it would likely decrease performance as the threads would contend heavily causing extensive cache ping-ponging and other issues. With so many threads, blocking most of them is the best strategy to avoid contention among the executing threads and allow this poorly-designed application to actually at least get some work done.

Again, if you really want this to work sensibly, you have to rearchitect it out of the insane thread count issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM