I am trying to start 10 threads strictly in turn using a semaphore. That is, after the execution of thread-0, thread-1 should be executed, but not thread-2.
But the problem is that the threads is arrives to the semaphore.acquire()-method in out of order, and therefore the execution of the threads is out of order.
How can I solve this problem with semaphore but without using thread.join()?
public class Main {
private Semaphore semaphore = new Semaphore(1, true);
public static void main(String[] args) {
new Main().start();
}
private void start() {
for (int i = 0; i < 10; i++) {
Runnable runnable = () -> {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("In run method " + Thread.currentThread().getName());
semaphore.release();
};
Thread thread = new Thread(runnable);
thread.start();
}
}
}
Output:
In run method Thread-0
In run method Thread-1
In run method Thread-4
In run method Thread-5
In run method Thread-3
In run method Thread-2
In run method Thread-6
In run method Thread-7
In run method Thread-9
In run method Thread-8