in my project I have until now "synchronized" multiple threads (each running the same type of Runnable) using a CyclicBarrier. In my case, using a CyclicBarrier turned out to be inefficient due to the high frequency of synchronizations, but a busy-wait mechanism might work faster. Here's what I got so far (some parts left out):
public class MyRunnable implements Runnable {
    private static AtomicInteger counter = null; // initialized to the number
                                                 // of threads
    public void run() {
        // do work up to a "common point"
        synchronized (this) {
            // decrement the counter and - if necessary - reset it
            if (counter.decrementAndGet() == 0) {
                counter.set(numberOfThreads);
                // make all the busy waiting threads exit from the loop
                for (int i = 0; i < threads.length; i++)
                    threads[i].interrupt();
            }
        }
        // busy wait until all threads have reached the "common point"
        while (!Thread.interrupted()) {}
    }
}
Unfortunately, this code performs even worse than CyclicBarrier. Here's a short, compilable example. Any suggestions on how to improve it?
 
     
     
     
    