停止当前线程,直到调用的线程完成,然后继续它
Stop current thread until the called thread is finished then continue it
假设我有这两种方法,它们一直在互相调用:
public void run() {
try {
Thread.sleep(1000);
System.out.println("Thread " +
Thread.currentThread().getId() +
" is running");
posses(numberOfPlayers, players);
} catch (Exception e) {
System.out.println("Exception Caught");
}
}
synchronized public void posses(int n, Players[] players) throws InterruptedException {
Random rand = new Random();
Scanner input = new Scanner(System.in);
for (int i = 0; i < players.length; i++) {
if (players[i].haveBall)
System.out.println("Player" + i + ", ");
}
System.out.println(" \bhave the ball now");
for (int i = 1; i < numberOfPlayers; i++) {
if (players[i].haveBall) {
System.out.println("\nList of available players to pass for player " + i + ": ");
for (int j = 1; j < numberOfPlayers; j++) {
if (players[j].isActive && j != i) {
System.out.println("Player " + players[j].number);
}
}
System.out.println();
do {
receiver = rand.nextInt(numberOfPlayers - 1) + 1;
} while (i == receiver && players[receiver].isActive);
if (players[receiver].haveBall) {
System.out.println("Player" + i + " passes");
System.out.println("Player" + receiver + " receives");
players[receiver].start();
players[i].join(2000);
players[i].haveBall = false;
players[receiver].haveBall = true;
players[receiver].players = players[0].players;
;
System.out.println();
for (int p = 1; p < n; p++) {
System.out.println(players[p].haveBall);
}
} else {
players[i].haveBall = false;
System.out.println("Player" + i + " passes");
players[receiver].haveBall = true;
System.out.println(players[receiver].number + " receives");
players[receiver].players = players[0].players;
System.out.println();
for (int p = 1; p < n; p++) {
System.out.println(players[p].haveBall);
}
players[receiver].start();
}
}
}
这是一支足球队,我们有两个球,每个球员最多可以有一个球。这些球员在不能将球传给当前有球的球员的情况下随机传球。
我这里有一个 for-loop,循环遍历球员并选择有球的球员并让他们传球。
我想做的是,如果我们要传给的球员有球,等他传球,然后再把球传给他。但是我写的线程的 join() 方法不能这样工作。(考虑每个玩家都是一个线程)。什么是最好的解决方案?
我会创建一个所有工作线程都可以访问的共享资源,一旦一个线程选择了一个资源,他就使用该资源进行工作,当他完成后,他 returns 将资源分配给共享资源池。
例如一个球队列,玩家查询队列以检查是否有可用的球,在拿起球时他们用球完成工作,然后将其放回队列中。这保证了每个球员一次 1 个球的基本要求。
还有几件事需要考虑:
- 线程饥饿,玩家不触球。处理这个问题的一种方法是在玩家拿起球后让线程休眠。
- 这个例子是共享资源的轮询方法,很容易实现你如何做“浪费”循环,其中线程检查资源是否可用,在你有 1 个球到 100 万玩家的场景中同样是非常浪费的可以有减少浪费的推动方法。
假设我有这两种方法,它们一直在互相调用:
public void run() {
try {
Thread.sleep(1000);
System.out.println("Thread " +
Thread.currentThread().getId() +
" is running");
posses(numberOfPlayers, players);
} catch (Exception e) {
System.out.println("Exception Caught");
}
}
synchronized public void posses(int n, Players[] players) throws InterruptedException {
Random rand = new Random();
Scanner input = new Scanner(System.in);
for (int i = 0; i < players.length; i++) {
if (players[i].haveBall)
System.out.println("Player" + i + ", ");
}
System.out.println(" \bhave the ball now");
for (int i = 1; i < numberOfPlayers; i++) {
if (players[i].haveBall) {
System.out.println("\nList of available players to pass for player " + i + ": ");
for (int j = 1; j < numberOfPlayers; j++) {
if (players[j].isActive && j != i) {
System.out.println("Player " + players[j].number);
}
}
System.out.println();
do {
receiver = rand.nextInt(numberOfPlayers - 1) + 1;
} while (i == receiver && players[receiver].isActive);
if (players[receiver].haveBall) {
System.out.println("Player" + i + " passes");
System.out.println("Player" + receiver + " receives");
players[receiver].start();
players[i].join(2000);
players[i].haveBall = false;
players[receiver].haveBall = true;
players[receiver].players = players[0].players;
;
System.out.println();
for (int p = 1; p < n; p++) {
System.out.println(players[p].haveBall);
}
} else {
players[i].haveBall = false;
System.out.println("Player" + i + " passes");
players[receiver].haveBall = true;
System.out.println(players[receiver].number + " receives");
players[receiver].players = players[0].players;
System.out.println();
for (int p = 1; p < n; p++) {
System.out.println(players[p].haveBall);
}
players[receiver].start();
}
}
}
这是一支足球队,我们有两个球,每个球员最多可以有一个球。这些球员在不能将球传给当前有球的球员的情况下随机传球。 我这里有一个 for-loop,循环遍历球员并选择有球的球员并让他们传球。 我想做的是,如果我们要传给的球员有球,等他传球,然后再把球传给他。但是我写的线程的 join() 方法不能这样工作。(考虑每个玩家都是一个线程)。什么是最好的解决方案?
我会创建一个所有工作线程都可以访问的共享资源,一旦一个线程选择了一个资源,他就使用该资源进行工作,当他完成后,他 returns 将资源分配给共享资源池。
例如一个球队列,玩家查询队列以检查是否有可用的球,在拿起球时他们用球完成工作,然后将其放回队列中。这保证了每个球员一次 1 个球的基本要求。
还有几件事需要考虑:
- 线程饥饿,玩家不触球。处理这个问题的一种方法是在玩家拿起球后让线程休眠。
- 这个例子是共享资源的轮询方法,很容易实现你如何做“浪费”循环,其中线程检查资源是否可用,在你有 1 个球到 100 万玩家的场景中同样是非常浪费的可以有减少浪费的推动方法。