Java - 线程执行顺序
Java - the thread execution order
我正在尝试使用信号量严格依次启动 10 个线程。
即thread-0执行完后,thread-1应该执行,thread-2不执行。
但问题是线程到达semaphore.acquire()
-方法时是乱序的,因此线程的执行是乱序的。
如何在不使用 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();
}
}
}
输出:
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
您需要一个具有某种排序概念的同步对象。如果您熟悉美国杂货店,请考虑熟食柜台的“取号”设备,它会告诉您轮到谁了。
代码粗略:
class SyncThing {
int turn = 0;
synchronized void waitForTurn(int me) {
while (turn != me)
wait();
}
synchronized void nextTurn() {
turn++;
notifyAll();
}
}
然后声明SyncThing syncThing = new SyncThing();
和 运行 第 i 个线程因此:
Runnable runnable = () -> {
syncThing.waitForTurn(i);
System.out.println("In run method " + Thread.currentThread().getName());
syncThing.nextTurn();
};
这是我随手输入的,并未作为完整代码提供,但它应该指明了方向。
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();
}
}
我正在尝试使用信号量严格依次启动 10 个线程。 即thread-0执行完后,thread-1应该执行,thread-2不执行。
但问题是线程到达semaphore.acquire()
-方法时是乱序的,因此线程的执行是乱序的。
如何在不使用 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();
}
}
}
输出:
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
您需要一个具有某种排序概念的同步对象。如果您熟悉美国杂货店,请考虑熟食柜台的“取号”设备,它会告诉您轮到谁了。
代码粗略:
class SyncThing {
int turn = 0;
synchronized void waitForTurn(int me) {
while (turn != me)
wait();
}
synchronized void nextTurn() {
turn++;
notifyAll();
}
}
然后声明SyncThing syncThing = new SyncThing();
和 运行 第 i 个线程因此:
Runnable runnable = () -> {
syncThing.waitForTurn(i);
System.out.println("In run method " + Thread.currentThread().getName());
syncThing.nextTurn();
};
这是我随手输入的,并未作为完整代码提供,但它应该指明了方向。
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();
}
}