It is hard to imagine that a busy-wait loop would be faster than not busy. First of all, in your code you still use at least the synchronization that you will need when using CyclicBarrier (see below). Secondly, you have just implemented the CyclicBarrier mechanism, in which Java developers spend time and energy optimizing for maximum performance. Thirdly, CyclicBarrier uses ReentrantLock for synchronization, which seems to be more efficient and faster than using the synchronized . Therefore, in general, it is unlikely that your code will win the race.
Consider this reference code:
public class MyRunnable implements Runnable { private static CyclicBarrier barrier = new CyclicBarrier(threads.length); public void run() {
In one run, this code will synchronize thread.length times, which is no more than in your version with the expected expectation. Therefore, it cannot be slower than your code.
The real cause of the performance problem is that your threads do not work much before they βmeetβ, which probably means that the overhead is with a high context flow and also a lot of synchronization.
Can you redefine architecture? Do you really need to wait for all employees to βmeetβ at a common point? Can you do a little more work before they "meet"? Have you tried setting the number of threads to a smaller number (= CPU / core count)?
Can you tell a little more about the purpose of the code and give a little more detailed information?
source share