我正在尝试创建一个应用程序,它将以类似方式在树中创建线程。我的主要方法是在Level0,它在Level1中创建线程。然后Level1将创建一些线程。 Level1中的每个线程将创建不同的线程集作为Level2,依此类推。
以下是我尝试使用的代码,使用ExecutorService:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadTree {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
//This is the main , level0
List ll = new ArrayList<>();
ll.add(2);
ll.add(5);
ll.add(8);
Iterator it = ll.iterator();
while (it.hasNext()) {
exec.submit(new Level1((Integer)it.next()));
}
}
}
class Level1 implements Runnable{
private ScheduledExecutorService exec;
private Integer num;
public Level1(Integer n){
num = n;
exec = Executors
.newScheduledThreadPool(n);
}
@Override
public void run() {
for(int i=0;i<num;i++){
exec.scheduleAtFixedRate(new Level2(), 0, 2, TimeUnit.SECONDS);
}
}
}
class Level2 implements Runnable{
@Override
public void run() {
System.out.println("Current Thread ID : " + Thread.currentThread().getId() + "Current Thread Name : "
+ Thread.currentThread().getName()) ;
//Do some task and create new threads
}
}
我有两个问题:
提前致谢。
答案 0 :(得分:1)
我认为你可以按照以下步骤实现这一目标 -
Runnable
对象,而Thread
是通过执行程序执行可运行任务的Java线程。)请参阅以下示例 -
public class ThreadTree {
private static final ExecutorService executor = Executors.newCachedThreadPool();
public static void main(String[] args) {
List<Integer> level1Nodes = new ArrayList<Integer>();
level1Nodes.add(2);
level1Nodes.add(5);
level1Nodes.add(8);
// start threads
for (Integer num : level1Nodes) {
executor.submit(new Level1(num));
}
}
private static class Notification {
private final Object result;
private final Exception rootException;
public Notification(Object result, Exception rootException) {
this.result = result;
this.rootException = rootException;
}
public Object getResult() {
return result;
}
public Exception getRootException() {
return rootException;
}
}
private static abstract class NotificationHandler {
private final AtomicInteger expectedNotifications;
private final List<Notification> notifications;
public NotificationHandler(int expectedNotifications) {
this.expectedNotifications = new AtomicInteger(expectedNotifications);
this.notifications = new ArrayList<Notification>();
}
public void handleNotification(Notification notification) {
notifications.add(notification);
if (expectedNotifications.decrementAndGet() == 0) {
postRun(notifications);
}
}
public void postRun(List<Notification> notifications) {
for (Notification notification : notifications) {
System.out.println("Status: " + (notification.getRootException() == null ? "Failed" : "Success") + ", Result: " + (notification.getResult() != null ? notification.getResult() : "No result"));
}
}
}
private static class Level1 extends NotificationHandler implements Runnable {
private final int num;
public Level1(int num) {
super(num);
this.num = num;
}
public void run() {
for (int i = 0; i < num; i++) {
executor.submit(new Level2(2, this)); // 2 is just an assumed number of nodes at level 2
}
}
}
private static class Level2 extends NotificationHandler implements Runnable {
private final int num;
private final NotificationHandler parentNotificationHandler;
public Level2(int num, NotificationHandler parentNotificationHandler) {
super(num);
this.num = num;
this.parentNotificationHandler = parentNotificationHandler;
}
public void run() {
for (int i = 0; i < num; i++) {
executor.submit(new Level2(2, this)); // 2 is just an assumed number of nodes at level 3
}
// execute the task and then notify parent
parentNotificationHandler.handleNotification(new Notification("done", null));
}
}
private static class Level3 extends NotificationHandler implements Runnable {
private final int num;
private final NotificationHandler parentNotificationHandler;
public Level3(int num, NotificationHandler parentNotificationHandler) {
super(num);
this.num = num;
this.parentNotificationHandler = parentNotificationHandler;
}
public void run() {
// execute the task and then notify parent
parentNotificationHandler.handleNotification(new Notification("done", null));
}
}
}
此Notification
传递来自Level3
- &gt; Level2' ->
级别1&#39 ;.每个子任务都有义务在完成自己的工作后通知父母。一旦通知所有子任务,父任务将执行运行后操作并通知其父项。
在线程池执行程序中使用哪些线程并不重要。唯一重要的是遵循每个子任务通知父母的规则,然后父母做后期操作并进一步通知其父母。
Notification
类由result
和rootException
组成,可以从子任务设置,以便父级可以知道子任务中出错的情况,并且异常可以传到最高级别。