当我运行以下代码时,8 个可用线程中只有 2 个运行,谁能解释为什么会这样?如何更改代码以利用所有 8 个线程?
Tree.java
:
package il.co.roy;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Tree<T>
{
private final T data;
private final Set<Tree<T>> subTrees;
public Tree(T data, Set<Tree<T>> subTrees)
{
this.data = data;
this.subTrees = subTrees;
}
public Tree(T data)
{
this(data, new HashSet<>());
}
public Tree()
{
this(null);
}
public T getData()
{
return data;
}
public Set<Tree<T>> getSubTrees()
{
return subTrees;
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Tree<?> tree = (Tree<?>) o;
return Objects.equals(data, tree.data) &&
Objects.equals(subTrees, tree.subTrees);
}
@Override
public int hashCode()
{
return Objects.hash(data, subTrees);
}
@Override
public String toString()
{
return "Tree{" +
"data=" + data +
", subTrees=" + subTrees +
'}';
}
public void sendCommandAll()
{
if (data != null)
System.out.println("[" + Thread.currentThread().getName() + "] sending command to " + data);
try
{
Thread.sleep(5000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
if (data != null)
System.out.println("[" + Thread.currentThread().getName() + "] tree with data " + data + " got " + true);
subTrees.parallelStream()
// .map(Tree::sendCommandAll)
.forEach(Tree::sendCommandAll);
// .reduce(true, (aBoolean, aBoolean2) -> aBoolean && aBoolean2);
}
}
forEach
(我使用or没关系reduce
)。
Main.java
:
package il.co.roy;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main
{
public static void main(String... args)
{
System.out.println("Processors: " + Runtime.getRuntime().availableProcessors());
final Tree<Integer> root = new Tree<>(null,
Set.of(new Tree<>(1,
IntStream.range(2, 7)
.boxed()
.map(Tree::new)
.collect(Collectors.toSet()))));
root.sendCommandAll();
// IntStream.generate(() -> 1)
// .parallel()
// .forEach(i ->
// {
// System.out.println(Thread.currentThread().getName());
// try
// {
// Thread.sleep(5000);
// } catch (InterruptedException e)
// {
// e.printStackTrace();
// }
// });
}
}
在该main
方法中,我创建了一个具有以下结构的树:\
root (data is `null`)
|- 1
|- 2
|- 3
|- 4
|- 5
|- 6
sendCommandAll
函数处理每个子树(并行)只有当它的父完成处理时。但结果如下:
处理器:8
[main] 向 1
[main] 树发送命令,数据 1 为真
[main] 向 6
[ForkJoinPool.commonPool-worker-2] 向 5
[main] 树发送命令,数据 6 为真
[ForkJoinPool .commonPool-worker-2] 数据 5 的树为真
[ForkJoinPool.commonPool-worker-2] 向 4 发送命令
[ForkJoinPool.commonPool-worker-2] 数据 4 的树为真
[ForkJoinPool.commonPool-worker-2]向 3
[ForkJoinPool.commonPool-worker-2] 树发送命令,数据 3 为真
[ForkJoinPool.commonPool-worker-2] 向 2
[ForkJoinPool.commonPool-worker-2] 树发送命令,数据 2 为真
(作为记录,当我执行注释代码时Main.java
,JVM 使用所有 7 (+ 1) 个可用线程commonPool
)
如何改进我的代码?