超线程 java_如果CPU是超线程,是否可以检查Java?

我想知道我可以运行的最佳线程数。通常,这等于Runtime.getRuntime()。availableProcessors()。

但是,返回的数字是支持超线程的CPU的两倍。现在,对于一些任务,超线程是好的,但对于别人来说,它什么都不做。在我的情况下,我怀疑它没有什么,所以我想知道是否必须将Runtime.getRuntime()。availableProcessors()返回的数字分成两部分。

为此,我必须推断CPU是否超线程。所以我的问题 – 我如何在Java中做?

谢谢。

编辑

好的,我已经对我的代码进行了基准测试。这是我的环境:

> Lenovo ThinkPad W510(即具有4个内核和超线程的i7 CPU),16G RAM

> Windows 7

> 84压缩CSV文件,压缩尺寸范围从105M到16M

>所有文件在主线程中逐个读取 – 不需要多线程访问HD。

>每个CSV文件行包含一些数据,这是被解析的,一个快速无上下文的测试确定该行是否相关。

>每个相关行包含两个双精度(代表好奇的经度和纬度),它们被强制为单个Long,然后存储在共享散列集中。

因此,工作线程不会从HD中读取任何内容,但是它们通过解压缩和解析内容来占用自己(使用opencsv库)。

下面是代码,没有无聊的细节:

public void work(File dir) throws IOException, InterruptedException {

Set allCoordinates = Collections.newSetFromMap(new ConcurrentHashMap());

int n = 6;

// NO WAITING QUEUE !

ThreadPoolExecutor exec = new ThreadPoolExecutor(n, n, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue());

StopWatch sw1 = new StopWatch();

StopWatch sw2 = new StopWatch();

sw1.start();

sw2.start();

sw2.suspend();

for (WorkItem wi : m_workItems) {

for (File file : dir.listFiles(wi.fileNameFilter)) {

MyTask task;

try {

sw2.resume();

// The only reading from the HD occurs here:

task = new MyTask(file, m_coordinateCollector, allCoordinates, wi.headerClass, wi.rowClass);

sw2.suspend();

} catch (IOException exc) {

System.err.println(String.format("Failed to read %s - %s", file.getName(), exc.getMessage()));

continue;

}

boolean retry = true;

while (retry) {

int count = exec.getActiveCount();

try {

// Fails if the maximum of the worker threads was created and all are busy.

// This prevents us from loading all the files in memory and getting the OOM exception.

exec.submit(task);

retry = false;

} catch (RejectedExecutionException exc) {

// Wait for any worker thread to finish

while (exec.getActiveCount() == count) {

Thread.sleep(100);

}

}

}

}

}

exec.shutdown();

exec.awaitTermination(1, TimeUnit.HOURS);

sw1.stop();

sw2.stop();

System.out.println(String.format("Max concurrent threads = %d", n));

System.out.println(String.format("Total file count = %d", m_stats.getFileCount()));

System.out.println(String.format("Total lines = %d", m_stats.getTotalLineCount()));

System.out.println(String.format("Total good lines = %d", m_stats.getGoodLineCount()));

System.out.println(String.format("Total coordinates = %d", allCoordinates.size()));

System.out.println(String.format("Overall elapsed time = %d sec, excluding I/O = %d sec", sw1.getTime() / 1000, (sw1.getTime() - sw2.getTime()) / 1000));

}

public class MyTask> implements Runnable {

private final byte[] m_buffer;

private final String m_name;

private final CoordinateCollector m_coordinateCollector;

private final Set m_allCoordinates;

private final Class m_headerClass;

private final Class m_rowClass;

public MyTask(File file, CoordinateCollector coordinateCollector, Set allCoordinates,

Class headerClass, Class rowClass) throws IOException {

m_coordinateCollector = coordinateCollector;

m_allCoordinates = allCoordinates;

m_headerClass = headerClass;

m_rowClass = rowClass;

m_name = file.getName();

m_buffer = Files.toByteArray(file);

}

@Override

public void run() {

try {

m_coordinateCollector.collect(m_name, m_buffer, m_allCoordinates, m_headerClass, m_rowClass);

} catch (IOException e) {

e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

}

}

}

请在下面找到结果(稍稍改变输出以省略重复的部分):

Max concurrent threads = 4

Total file count = 84

Total lines = 56395333

Total good lines = 35119231

Total coordinates = 987045

Overall elapsed time = 274 sec, excluding I/O = 266 sec

Max concurrent threads = 6

Overall elapsed time = 218 sec, excluding I/O = 209 sec

Max concurrent threads = 7

Overall elapsed time = 209 sec, excluding I/O = 199 sec

Max concurrent threads = 8

Overall elapsed time = 201 sec, excluding I/O = 192 sec

Max concurrent threads = 9

Overall elapsed time = 198 sec, excluding I/O = 186 sec

你可以自由地得出自己的结论,但是我的这个超线程确实会改善我在具体情况下的表现。另外,有6个工作线程似乎是这个任务和我的机器的正确选择。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值