+1 vote
in JAVA by
What best practices you follow while writing multi-threaded code in Java?

1 Answer

0 votes
by

Here are couple of best practices which I follow while writing concurrent code in Java:

a) Always name your thread, this will help in debugging.

b) minimize the scope of synchronization, instead of making whole method synchronized, only critical section should be synchronized.

c) prefer volatile over synchronized if you can can.

e) use higher level concurrency utilities instead of waitn() and notify for inter thread communication e.g. BlockingQueue, CountDownLatch and Semeaphore.

e) Prefer concurrent collection over synchronized collection in Java. They provide better scalability.

Related questions

0 votes
asked Oct 3, 2023 in Service Now by john ganales
0 votes
asked Aug 26, 2023 in Apache Superset by rajeshsharma
...