我正在嘗試使用兩個執行緒相互交替以計數到某個亂數。例如,如果執行緒 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();
int random = rand.nextInt(20) 1; // generate random number 1-20
for (int i = 0; i < random; i ) {
synchronized (sync) {
try {
sync.notify();
System.out.println(i);
sync.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
我已經有一段時間處于死胡同了,我不知道該怎么辦。這是我第一次使用執行緒,所以我可以使用一些幫助。如果有人知道如何做到這一點,請告訴我,因為我已經嘗試了很多但沒有任何效果。提前致謝。
uj5u.com熱心網友回復:
這是一點點,基本的。我不喜歡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();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/349081.html
