當我運行以下代碼時,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或都沒有關系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函式處理每個子樹(并行)只有當它的父級完成處理時。但結果如下:
Processors: 8
[main] sending command to 1
[main] tree with data 1 got true
[main] sending command to 6
[ForkJoinPool.commonPool-worker-2] sending command to 5
[main] tree with data 6 got true
[ForkJoinPool.commonPool-worker-2] tree with data 5 got true
[ForkJoinPool.commonPool-worker-2] sending command to 4
[ForkJoinPool.commonPool-worker-2] tree with data 4 got true
[ForkJoinPool.commonPool-worker-2] sending command to 3
[ForkJoinPool.commonPool-worker-2] tree with data 3 got true
[ForkJoinPool.commonPool-worker-2] sending command to 2
[ForkJoinPool.commonPool-worker-2] tree with data 2 got true
(For the record, when I execute the commented code in Main.java, the JVM uses all 7 ( 1) threads available commonPool)
How can I improve my code?
uj5u.com熱心網友回復:
如本答案后半部分所述,處理HashMaps 或HashSets時的執行緒利用率取決于支持陣列中元素的分布,后者取決于哈希碼。特別是在元素數量較少的情況下,與(默認)容量相比,這可能會導致糟糕的作業拆分。
一個簡單的解決方法是使用new ArrayList<>(subTrees).parallelStream()代替subTrees.parallelStream().
但請注意,您的方法sleep在處理子節點之前執行當前節點的實際作業(在使用 a 模擬的示例中),這也降低了潛在的并行度。
您可以使用
public void sendCommandAll() {
if(subTrees.isEmpty()) {
actualSendCommand();
return;
}
List<Tree<T>> tmp = new ArrayList<>(subTrees.size() 1);
tmp.addAll(subTrees);
tmp.add(this);
tmp.parallelStream().forEach(t -> {
if(t != this) t.sendCommandAll(); else t.actualSendCommand();
});
}
private void actualSendCommand() {
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);
}
這允許在處理子節點的同時處理當前節點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318751.html
標籤:java multithreading java-8 java-stream java-17
下一篇:如何同步慢計算并快取它?
