gpt4 book ai didi

java - 同步改进以按特定顺序实现多线程完成

转载 作者:行者123 更新时间:2023-11-30 07:19:02 25 4
gpt4 key购买 nike

问题陈述:公共(public)汽车可以按任何顺序启动,但会按特定顺序到达目的地。因此,如果之前预期的巴士尚未到达,巴士就会等待。

在遇到多个丢失信号并解决它们之后,我终于想出了以下解决方案。我的问题:首先,我们如何修改下面的代码以使其从设计角度变得健壮?其次,JAVA中有没有其他同步机制可以解决这个问题?

public class BusDestination implements Runnable {

private boolean[] busArrivalStatus;
private List<String> busSeq = new ArrayList<>();


private Lock lock = new ReentrantLock();
private Condition prevBusFlag = lock.newCondition();
private CountDownLatch latch;
boolean signalled = false;

public BusDestination(CountDownLatch latch) {
busSeq.add("B1");
busSeq.add("B2");
busSeq.add("B3");
busSeq.add("B4");
busSeq.add("B5");

busArrivalStatus = new boolean[busSeq.size()];
for (int i = 0; i < this.busSeq.size(); i++) {
busArrivalStatus[i] = false;
}
this.latch = latch;
}


public void run() {

String busName = Thread.currentThread().getName();

try{
lock.lock();
int busArrrivalSeq = busSeq.indexOf(busName);
if(busArrrivalSeq==0){
//first bus arrived
System.out.println("******** Bus arriaved : "+busName);
busArrivalStatus[0] = true;
System.out.println(busName+" will signall");
signalled = true;
prevBusFlag.signalAll();
} else {
while(!isValidSeq(busName) && !signalled ){
System.out.println(busName+" going to wait.");
prevBusFlag.await();
}
System.out.println("******** Bus arriaved : "+busName);
busArrivalStatus[busSeq.indexOf(busName)] = true;
signalled = true;//getPreviousBusStatus(busName);
prevBusFlag.signalAll();
}
} catch(Exception ex){
ex.printStackTrace();
System.out.println("Except--"+busName);
}finally{
lock.unlock();
}
latch.countDown();
}

private boolean isValidSeq(String busName) {
int prevIndex = busSeq.indexOf(busName)-1;
if(!busArrivalStatus[prevIndex]){
signalled = false;
}
return busArrivalStatus[prevIndex];
}

public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(5);

BusDestination destination = new BusDestination(latch);

Thread b1 = new Thread(destination);
Thread b2 = new Thread(destination);
Thread b3 = new Thread(destination);
Thread b4 = new Thread(destination);
Thread b5 = new Thread(destination);

b1.setName("B1");
b2.setName("B2");
b3.setName("B3");
b4.setName("B4");
b5.setName("B5");

b4.start();
b5.start();
b3.start();
b1.start();
b2.start();

latch.await();
}

}

最佳答案

这可以通过仅使用 CountDownLatch 实例来简化,而不需要锁实例。您试图解决的问题是线程 n 可以同时执行,但在某个点线程 t 必须等待,直到线程 t-1 到达某个点。这是我的解决方案。

public class BusDestination {

private CountDownLatch pre, next;

public BusDestination(CountDownLatch pre, CountDownLatch next) {
this.pre = pre;
this.next = next;
}

public void run() {
// Do work....
pre.await(); // wait until prior thread{s} are done
// If required, do more work...
pre.countDown(); // notify next set of thread{s} that you are done
// If required, do more work...
}

public static void main(String args[]) {
CountDownLatch first = new CountDownLatch(0); // await will return right away.
CountDownLatch second = new CountDownLatch(1);
// Continue creating CountDownLatch....
CountDownLatch preLast = new CountDownLatch(1);
CountDownLatch last = new CountDownLatch(1); // used to wait by the main thread.

BusDestination firstBus = new BusDestination(first, second);
// create more destinations
BusDestination lastBus = new BusDestination(preLast, last);

// start threads....

last.await(); // Waits until all of the threads complete

}

此解决方案的一个很好的功能是,您可以创建一个管道,其中步骤 n 必须等待两个或多个线程在步骤 n - 1 中完成才能继续。

关于java - 同步改进以按特定顺序实现多线程完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37905307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com