Java 线程使用固定线程池和大量任务
我正在用一个程序运行数学中的Collatz猜想()。我实现了一个类,它运行推测算法(并返回输出),一个创建固定线程池(处理器数量为8),并接受Java 线程使用固定线程池和大量任务,java,multithreading,performance,algorithm,math,Java,Multithreading,Performance,Algorithm,Math,我正在用一个程序运行数学中的Collatz猜想()。我实现了一个类,它运行推测算法(并返回输出),一个创建固定线程池(处理器数量为8),并接受Callables,这是对推测算法的调用 我为1(输入类型必须是正整数)和400000之间的所有数字创建了一个HashSet。这(似乎)永远挂起,但较低的数字效果很好,这很奇怪。更奇怪的是,运行它处理这些调用所需的时间似乎比单个线程处理相同数量的信息所需的时间更长;它也会大大增加记忆 例如,在我的计算机上,程序用不到一秒钟的时间执行400000(最终值)的
Callable
s,这是对推测算法的调用
我为1(输入类型必须是正整数)和400000之间的所有数字创建了一个HashSet
。这(似乎)永远挂起,但较低的数字效果很好,这很奇怪。更奇怪的是,运行它处理这些调用所需的时间似乎比单个线程处理相同数量的信息所需的时间更长;它也会大大增加记忆
例如,在我的计算机上,程序用不到一秒钟的时间执行400000(最终值)的算法(仅一次迭代),所有较低的值用更少的时间进行计算(可能除了需要更长时间的素数),我运行的是8GB ram的Windows 8.1,以及2.2Ghz的8个逻辑处理器
代码:
private static void initThreads()引发InterruptedException{
//创建目录(SEQUENCER\u文件夹\u路径);
//createFile(SEQUENCER\u FILE\u路径);
ExecutorService=Executors.newFixedThreadPool(8,new ThreadFactory()){
私有BigInteger计数=BigInteger.0;
@凌驾
公共线程newThread(可运行的r){
count=count.add(biginger.ONE);
返回新线程(r,“Collatz Sequencer线程:”+计数);
}
});
int finalNumber=400_000;
最终哈希集任务=新哈希集(最终编号);
对于(长l=1;长l{
CollatzSequencer sequencer=新的CollatzSequencer(新的BigInteger(number.toString());
已同步(数据集){
put(number,sequencer.init());
}
返回null;
});
}
service.invokeAll(任务);
线程数据线程=新线程(()->{
while(true){
已同步(数据集){
if(dataSet.size()==finalNumber){
System.err.println(“值:\n”);
对于(CollatzSequencer.FinalSequencerReport数据:dataSet.values()){
System.err.println(“条目:“+data.getInitialValue()+”,“+data.getIterations());
}
系统出口(0);
}
}
}
},“Collatz猜想数据集线程”);
dataThread.start();
}
Collatz猜想算法:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.collatzsequencer.core;
import java.math.BigInteger;
/**
* A sequencer used for computing the collatz sequence.
*
* @author Sarah Szabo
* @version 1.0
*/
public class CollatzSequencer {
private final BigInteger initialValue;
public CollatzSequencer(BigInteger currentValue) {
if (currentValue == null) {
throw new NullPointerException("Value passed can't be null");
} else if (currentValue.compareTo(new BigInteger("1")) < 0) {
throw new NumberFormatException("The value passed to the constructor must be a natural number.");
}
this.initialValue = currentValue;
}
public FinalSequencerReport init() {
return new FinalSequencerReport(performOperation(new SequencerReport(this.initialValue)), this.initialValue);
}
private SequencerReport performOperation(SequencerReport report) {
if (report.getResult().equals(new BigInteger("1"))) {
return new SequencerReport(report.getResult(), report.getIterations(), report.getSequence().length() > 1
? report.getSequence().substring(0, report.getSequence().length() - 3) : "The sequence starts and ends at 1 <Nothing Done>");
} else if (report.getResult().mod(new BigInteger("2")).equals(new BigInteger("0"))) {
BigInteger value = report.getResult().divide(new BigInteger("2"));
return performOperation(new SequencerReport(value, report.getIterations().add(new BigInteger("1")),
report.getSequence() + " " + report.getResult() + "/2 -> " + value + " ->"));
} else {
BigInteger value = report.getResult().multiply(new BigInteger("3")).add(new BigInteger("1"));
return performOperation(new SequencerReport(value, report.getIterations()
.add(new BigInteger("1")), report.getSequence() + report.getResult() + " * 3 + 1 ->" + value + " ->"));
}
}
public static final class FinalSequencerReport extends SequencerReport {
private final BigInteger initialValue;
private final String finalFormattedString;
public FinalSequencerReport(SequencerReport finalReport, BigInteger initialValue) {
super(finalReport.getResult(), finalReport.getIterations(), finalReport.getSequence());
this.initialValue = initialValue;
this.finalFormattedString = "Initial Value: "
+ getInitialValue() + "\nFinal Value: " + getResult() + "\nIterations: "
+ getIterations() + "\nAlgebraic Sequence:\n" + getSequence();
}
public String getFinalFormattedString() {
return finalFormattedString;
}
public BigInteger getInitialValue() {
return initialValue;
}
}
public static class SequencerReport {
private final BigInteger result, iterations;
private final String sequence;
public SequencerReport(BigInteger result) {
this(result, new BigInteger("0"), "");
}
public SequencerReport(BigInteger result, BigInteger iterations, String sequence) {
this.result = result;
this.iterations = iterations;
this.sequence = sequence;
}
public BigInteger getResult() {
return this.result;
}
public BigInteger getIterations() {
return this.iterations;
}
public String getSequence() {
return this.sequence;
}
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包com.collatzsequencer.core;
导入java.math.biginger;
/**
*用于计算collatz序列的定序器。
*
*@作者莎拉·萨博
*@version 1.0
*/
公共类序列器{
私有最终BigInteger初始值;
公共序列器(BigInteger currentValue){
if(currentValue==null){
抛出新的NullPointerException(“传递的值不能为null”);
}else if(currentValue.compareTo(新的BigInteger(“1”))<0){
抛出新的NumberFormatException(“传递给构造函数的值必须是自然数”);
}
this.initialValue=当前值;
}
public FinalSequencerReport init(){
返回新的FinalSequencerReport(执行操作(新的SequencerReport(this.initialValue)),this.initialValue);
}
专用SequencerReport性能操作(SequencerReport报告){
if(report.getResult()等于(新的BigInteger(“1”)){
返回新的SequencerReport(report.getResult()、report.getIterations()、report.getSequence().length()>1
?report.getSequence().substring(0,report.getSequence().length()-3):“序列开始和结束于1”);
}else if(report.getResult().mod(新的BigInteger(“2”)).equals(新的BigInteger(“0”)){
BigInteger值=report.getResult().divide(新的BigInteger(“2”));
返回performOperation(新的SequencerReport(值,report.getIterations().add(新的BigInteger(“1”)),
report.getSequence()+“”+report.getResult()+”/2->“+value+”->”);
}否则{
BigInteger值=report.getResult().multiply(新的BigInteger(“3”)).add(新的BigInteger(“1”);
返回performOperation(新的SequencerReport(值,report.getIterations()
.add(新的BigInteger(“1”)),report.getSequence()+report.getResult()+“*3+1->”+value+“->”);
}
}
公共静态最终类FinalSequencerReport扩展SequencerReport{
私有最终BigInteger初始值;
私有最终字符串最终格式字符串;
公共FinalSequencerReport(SequencerReport finalReport,BigInteger初始值){
super(finalReport.getResult(),finalReport.getIterations(),finalReport.getSequence());
this.initialValue=initialValue;
this.finalFormattedString=“初始值:”
+getInitialValue()+“\n最终值:“+getResult()+”\n操作:
+getIterations()+“\n内存序列:\n”+getSequence();
}
公共字符串getFinalFormattedString(){
返回最终格式字符串;
}
public BigInteger getInitialValue(){
返回初始值;
}
}
公共静态类SequencerReport{
私有最终BigInteger结果,迭代;
私有最终字符串序列;
公共SequencerReport(BigInteger结果){
这(结果,新的BigInteger(“0”),“”);
}
公共SequencerReport(BigInteger结果、BigInteger迭代、字符串序列){
this.result=结果;
this.iterations=迭代次数;
这个序列=序列;
}
public BigInteger getResult(){
返回此结果;
}
公共BigInteger getIterations(){
返回
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.collatzsequencer.core;
import java.math.BigInteger;
/**
* A sequencer used for computing the collatz sequence.
*
* @author Sarah Szabo
* @version 1.0
*/
public class CollatzSequencer {
private final BigInteger initialValue;
public CollatzSequencer(BigInteger currentValue) {
if (currentValue == null) {
throw new NullPointerException("Value passed can't be null");
} else if (currentValue.compareTo(new BigInteger("1")) < 0) {
throw new NumberFormatException("The value passed to the constructor must be a natural number.");
}
this.initialValue = currentValue;
}
public FinalSequencerReport init() {
return new FinalSequencerReport(performOperation(new SequencerReport(this.initialValue)), this.initialValue);
}
private SequencerReport performOperation(SequencerReport report) {
if (report.getResult().equals(new BigInteger("1"))) {
return new SequencerReport(report.getResult(), report.getIterations(), report.getSequence().length() > 1
? report.getSequence().substring(0, report.getSequence().length() - 3) : "The sequence starts and ends at 1 <Nothing Done>");
} else if (report.getResult().mod(new BigInteger("2")).equals(new BigInteger("0"))) {
BigInteger value = report.getResult().divide(new BigInteger("2"));
return performOperation(new SequencerReport(value, report.getIterations().add(new BigInteger("1")),
report.getSequence() + " " + report.getResult() + "/2 -> " + value + " ->"));
} else {
BigInteger value = report.getResult().multiply(new BigInteger("3")).add(new BigInteger("1"));
return performOperation(new SequencerReport(value, report.getIterations()
.add(new BigInteger("1")), report.getSequence() + report.getResult() + " * 3 + 1 ->" + value + " ->"));
}
}
public static final class FinalSequencerReport extends SequencerReport {
private final BigInteger initialValue;
private final String finalFormattedString;
public FinalSequencerReport(SequencerReport finalReport, BigInteger initialValue) {
super(finalReport.getResult(), finalReport.getIterations(), finalReport.getSequence());
this.initialValue = initialValue;
this.finalFormattedString = "Initial Value: "
+ getInitialValue() + "\nFinal Value: " + getResult() + "\nIterations: "
+ getIterations() + "\nAlgebraic Sequence:\n" + getSequence();
}
public String getFinalFormattedString() {
return finalFormattedString;
}
public BigInteger getInitialValue() {
return initialValue;
}
}
public static class SequencerReport {
private final BigInteger result, iterations;
private final String sequence;
public SequencerReport(BigInteger result) {
this(result, new BigInteger("0"), "");
}
public SequencerReport(BigInteger result, BigInteger iterations, String sequence) {
this.result = result;
this.iterations = iterations;
this.sequence = sequence;
}
public BigInteger getResult() {
return this.result;
}
public BigInteger getIterations() {
return this.iterations;
}
public String getSequence() {
return this.sequence;
}
}
}