Java 如何在线程迭代前后打印一些消息?
我昨天问了关于线程同步的问题 最后,同步问题得到了解决,但我必须在线程迭代之前/之后打印Java 如何在线程迭代前后打印一些消息?,java,multithreading,Java,Multithreading,我昨天问了关于线程同步的问题 最后,同步问题得到了解决,但我必须在线程迭代之前/之后打印 for(int i=0; i<5; i++) { CarSensors= new Thread[]{ new Thread(frontCarSensor), new Thread(leftRightCarSensor), new Thread(LaneSensor), new Thread(SignalSensor), new Thr
for(int i=0; i<5; i++) {
CarSensors=
new Thread[]{
new Thread(frontCarSensor),
new Thread(leftRightCarSensor),
new Thread(LaneSensor),
new Thread(SignalSensor),
new Thread(PedestrianSensor),
new Thread(ObjectSensor)
};
for(Thread t:CarSensors) t.start();
}
下面是代码,它只是一个迭代
for(int i=0; i<5; i++) {
CarSensors=
new Thread[]{
new Thread(frontCarSensor),
new Thread(leftRightCarSensor),
new Thread(LaneSensor),
new Thread(SignalSensor),
new Thread(PedestrianSensor),
new Thread(ObjectSensor)
};
for(Thread t:CarSensors) t.start();
}
您需要在
CarSensoer
的代码中添加打印。如果线程构造函数中的每个参数都是可运行类,则可以在run()方法中添加打印语句
我可能不会直接在Java中使用
Thread
,除非我正在编写并发构建块。此外,与您可以通过编程方式执行的大多数其他操作相比,线程非常昂贵,因此在可能的情况下,应该避免动态创建线程(尤其是在循环中)
在主题案例中,解决方案可能是使用CompletableFuture
和Executors
框架的功能(下面的基本示例)
CompletableFuture允许注册完成挂钩,因此任何类型的遥测都可以围绕主逻辑进行,而不必触及后者
防止消息重叠,将代理打印到单线程执行器,并按时间戳、汽车ID或任何其他适当的方式对其进行排序
import static java.util.concurrent.CompletableFuture.runAsync;
// You can use JDK Executors, Guava Executors,
// or write your own taking the concurrency consideration
// away from the business logic
final Executor executor = Executors.newFixedThreadPool(N);
CompletableFuture collector = CompletableFuture.completed((Void)null);
for (int i = 0; i < 5; ++i) {
collector = CompletableFuture.allOf(
collector,
runAsync(frontCarSensor, executor),
runAsync(leftRightCarSensor, executor),
runAsync(LaneSensor, executor),
runAsync(SignalSensor, executor),
runAsync(PedestrianSensor, executor),
runAsync(ObjectSensor, executor)).join();
}
collector.join();
导入静态java.util.concurrent.CompletableFuture.runAsync;
//你可以使用JDK执行器,番石榴执行器,
//或者在考虑并发性的情况下编写自己的代码
//远离业务逻辑
final Executor Executor=Executors.newFixedThreadPool(N);
CompletableFuture收集器=CompletableFuture.completed((Void)null);
对于(int i=0;i<5;++i){
收集器=CompletableFuture.allOf(
收藏家,
runAsync(前端传感器、执行器),
runAsync(leftRightCarSensor、executor),
runAsync(LaneSensor,executor),
runAsync(信号传感器、执行器),
runAsync(行人传感器、执行器),
runAsync(ObjectSensor,executor)).join();
}
collector.join();
CarSensor
是线程的数组变量。我可以在里面添加打印消息吗?为什么不使用更高级别的并发API,比如completable future?这会让你更容易加入/等待。。。否则,您需要在该数组中的每个线程上调用join
,以在其完成后继续。@ErnestKiwele I Edited您可以展示传感器类的外观吗(例如frontCarSensor)?@xagaffar我补充道,我更希望看到您的run方法实现结构是每个~sensor
对象都扩展了sensor
类,该类实现了Runnable
接口。根据这个概念,解决方案是什么?传感器类的子类是6。但执行顺序是任意的,因为它们是不同的线程。由于任意执行,您的解决方案无法解决我的问题。每6个对象都必须作为一个单元执行,并且打印消息必须在开始时打印,而结尾不清楚您期望的是什么,但是从您的消息中我可以推断,您希望所有消息按照执行顺序排列。如果是这种情况,您必须在同步模式下运行这些消息,通过使用倒计时锁存器,异步模式不适合您。
import static java.util.concurrent.CompletableFuture.runAsync;
// You can use JDK Executors, Guava Executors,
// or write your own taking the concurrency consideration
// away from the business logic
final Executor executor = Executors.newFixedThreadPool(N);
CompletableFuture collector = CompletableFuture.completed((Void)null);
for (int i = 0; i < 5; ++i) {
collector = CompletableFuture.allOf(
collector,
runAsync(frontCarSensor, executor),
runAsync(leftRightCarSensor, executor),
runAsync(LaneSensor, executor),
runAsync(SignalSensor, executor),
runAsync(PedestrianSensor, executor),
runAsync(ObjectSensor, executor)).join();
}
collector.join();