[英]Switching between threads and letting loop finish Java
我正在嘗試使用兩個線程相互交替以計數到某個隨機數。 例如,如果線程 A 被分配了一個隨機數 4 而線程 B 被分配了一個隨機數 7,我正在尋找這樣的輸出:
Thread A:1
Thread B:1
Thread A:2
Thread B:2
Thread A:3
Thread B:3
Thread A:4
Thread B:4
Thread A:5
Thread A:6
Thread A:7
所以基本上它會在線程之間切換,如果一個線程完成對隨機數的計數,它將停止並讓另一個線程完成對隨機數的計數。 這是我到目前為止的代碼:
public class Test extends Thread {
public static Object sync = new Object();
public void run() {
Random rand = new Random();
// generate random number 1-20;
int random = rand.nextInt((20 - 1) + 1) + 1;
for (int i = 0; i < random; i++) {
synchronized (sync) {
try {
sync.notify();
System.out.println(i);
sync.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
我已經有一段時間處於死胡同了,我不知道該怎么辦。 這是我第一次使用線程,所以我可以使用一些幫助。 如果有人知道如何做到這一點,請告訴我,因為我已經嘗試了很多,但沒有任何效果。 提前致謝。
這是一點點,基本的。 在這種情況下,我不喜歡static
,但為了演示,它應該有助於證明這一點
本質上,您需要一個“計數器”來跟蹤“活動”線程的數量,如果只有一個,則不需要“等待”,例如......
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.DataFormatException;
public class Main {
public static void main(String[] args) throws IOException, DataFormatException {
new Thread(new Test(), "One").start();
new Thread(new Test(), "Two").start();
}
public static class Test implements Runnable {
public static Object sync = new Object();
public static AtomicInteger counter = new AtomicInteger(0);
public void run() {
counter.incrementAndGet();
Random rand = new Random();
// generate random number 1-20;
int random = rand.nextInt((20 - 1) + 1) + 1;
String name = Thread.currentThread().getName();
System.out.println(name + " count to " + random);
for (int i = 0; i < random; i++) {
synchronized (sync) {
try {
sync.notify();
System.out.println(name + " " + i);
int count = counter.get();
if (count > 1) {
sync.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
counter.decrementAndGet();
synchronized (sync) {
sync.notify();
}
}
}
}
而且,因為我不喜歡濫用static
(在這種情況下可以爭論sync
,但作為基本概念的演示)
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.DataFormatException;
public class Main {
public static void main(String[] args) throws IOException, DataFormatException {
Object lock = new Object();
AtomicInteger counter = new AtomicInteger(0);
new Thread(new Test(lock, counter), "One").start();
new Thread(new Test(lock, counter), "Two").start();
}
public static class Test implements Runnable {
private Object lock;
private AtomicInteger counter;
public Test(Object lock, AtomicInteger counter) {
this.lock = lock;
this.counter = counter;
}
public void run() {
counter.incrementAndGet();
Random rand = new Random();
// generate random number 1-20;
int random = rand.nextInt((20 - 1) + 1) + 1;
String name = Thread.currentThread().getName();
System.out.println(name + " count to " + random);
for (int i = 0; i < random; i++) {
synchronized (lock) {
try {
lock.notify();
System.out.println(name + " " + i);
int count = counter.get();
if (count > 1) {
lock.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
counter.decrementAndGet();
synchronized (lock) {
lock.notify();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.