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
You need a synchronization object with some sort of concept of ordering. If you're familiar with US grocery stores, consider the "take a number" device at the deli counter that tells you whose turn it is.
Rough sketch of code:
class SyncThing {
int turn = 0;
synchronized void waitForTurn(int me) {
while (turn != me)
wait();
}
synchronized void nextTurn() {
turn++;
notifyAll();
}
}
then declare SyncThing syncThing = new SyncThing();
and run the i'th thread thus:
Runnable runnable = () -> {
syncThing.waitForTurn(i);
System.out.println("In run method " + Thread.currentThread().getName());
syncThing.nextTurn();
};
This is typed in off the top of my head and is not offered as complete code, but it should show the way.
private void start() {
final AtomicInteger counter = new AtomicInteger();
for (int i = 0; i < 10; i++) {
final int num = i;
new Thread(() -> {
while (counter.get() != num) {
}
System.out.println("In run method " + Thread.currentThread().getName());
counter.incrementAndGet();
}).start();
}
}
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.