- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的并行 Java 代码有问题。我尝试从磁盘读取一些图像,更改图像的名称,然后将它们再次保存到不同的文件夹中。为此,我尝试按如下方式并行运行它:
int nrOfThreads = Runtime.getRuntime().availableProcessors();
int nrOfImagesPerThread = Math.round(remainingImages.size()/((float)nrOfThreads));
ExecutorService ex2 = Executors.newFixedThreadPool(nrOfThreads);
int indexCounter = 0;
for(int i = 0; i< nrOfThreads; ++i) {
if(i != (nrOfThreads-1)) {
ex2.execute(new ImageProcessing(remainingImages.subList(indexCounter, indexCounter+nrOfImagesPerThread), newNames.subList(indexCounter,indexCounter+nrOfImagesPerThread)));
indexCounter+=nrOfImagesPerThread;
}else {
ex2.execute(new ImageProcessing(remainingImages.subList(indexCounter, remainingImages.size()), newNames.subList(indexCounter,remainingImages.size())));
}
}
ex2.shutdown();
try {
ex2.awaitTermination(12, TimeUnit.HOURS);
} catch (InterruptedException e) {
e.printStackTrace();
}
这是 ImageProcessing 类:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
public class ImageProcessing implements Runnable {
private List<String> oldPaths;
private List<String> newPaths;
public ImageProcessing(List<String> oldPaths, List<String> newPaths) {
this.oldPaths = oldPaths;
this.newPaths = newPaths;
}
@Override
public void run() {
for(int i = 0; i< oldPaths.size();++i) {
try {
BufferedImage img = ImageIO.read(new File(oldPaths.get(i)));
File output = new File(newPaths.get(i));
ImageIO.write(img, "jpg", output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我将 for 循环中的图像位置分成(线程数)部分,所以在我的例子中大约有 8 个部分。当我现在运行代码时,它确实是并行运行的,但它并没有使用 100% 的 cpu 能力。它只使用每个处理器的大约 25%。
有人知道为什么会这样吗?还是我只是在编程的某个地方搞砸了?
非常感谢!
编辑:为了让寻求相同功能的人完成任务,我查看了 Apache 公共(public)库 (see here) 并找到了一种不错且更快的方法来从中复制图像一个硬盘到另一个。 ImageProcessing 类现在如下所示:
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class ImageProcessing implements Runnable {
private List<String> oldPaths;
private List<String> newPaths;
public ImageProcessing(List<String> oldPaths, List<String> newPaths) {
this.oldPaths = oldPaths;
this.newPaths = newPaths;
}
@Override
public void run() {
for(int i = 0; i< oldPaths.size();++i) {
File sourceFile = new File(oldPaths.get(i));
File targetFile = new File(newPaths.get(i));
//copy file from one location to other
try {
FileUtils.copyFile(sourceFile, targetFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
最佳答案
你的问题是,这里的瓶颈肯定是磁盘的 I/O。您可能需要在没有 ExecutorService 的情况下同时重命名文件。
换句话说:将更改(重命名文件)写入磁盘所消耗的时间比 CPU 使用的时间还多。
你不能多线程这样的 Action 。
只需测量代码的串行(非多线程)版本所需的时间,并将其与多线程代码所需的时间进行比较。它或多或少是一样的。
关于java - 使用 ExecutorService : Doesn't use 100% of my cpu-power 在 Java 中并行重命名图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15161272/
Power Pivot、Power Query 和 Power BI 之间有什么区别?应该如何决定应该使用哪种工具以及何时使用。 最佳答案 电源查询 Power Query 是一种自助式 ETL(提取
在 Microsoft Power Automate 中,使用表达式 utcNow() 可以获得当前日期(和时间)。我正在尝试获取昨天的日期。我尝试了 dateadd(utcNow(), -1) 和类
也许这是一个非常简单的问题,但我试图弄清楚如何做到这一点,因为我有数百列以及手动完成的想法,将它们分成单独的查询然后 append 它们似乎并不非常实用。 我一直在处理一个查询,它以以下格式返回值:
我使用 Power Automate Desktop 创建了一个桌面流程。但我无法安排或让它自动运行。有什么办法吗? 我不希望使用云流和使用网关连接桌面流。我需要在我的台式机本身内自动运行桌面流程。有
我有一个数据集,它在 SQL Server 中进行转换,然后发送到 Power BI。该报告是根据营销人员的规范制定的,因此我无法表现出色(需要漂亮)。 是否有人设置了自动导出 PBI 报告(按特定列
所以我问了一个类似的问题,但我想我应该更普遍地提出这个问题,以获得尽可能多的想法。 我有 Power BI Pro。我的任务是为数百个收件人创建报告,每个报告都针对该特定用户进行个性化设置。 尽管每个
假设我正在将以下内容导入 PowerBI: Date | Quantity |---------------------|------------
我使用 Power Automate Desktop 创建了一个桌面流程。但我无法安排或让它自动运行。有什么办法吗? 我不希望使用云流和使用网关连接桌面流。我需要在我的台式机本身内自动运行桌面流程。有
我在表格中有一列,如下所示 当我使用 Replace Errors 将类型更改为 decimal(类型编号)并将其替换为 0.0 时,此列中的字符串给了我错误。 然后我旋转了专栏帖子,该专栏如下所示:
我对 Power BI 和 Power Automate 非常缺乏经验,如果这个问题有简单的答案(至少我找不到),我深表歉意。 我有一个 python 脚本,它从一些 excel 文件中获取数据,创建
将数据加载到模型后,我需要删除查询步骤。原因是隐藏消息来源,保护我们的专有技术,或者我只是对我所做的事情并不感到自豪;)。 但是当我删除 PQ 连接或更改“加载到”选项时,表格也会从数据模型中消失,并
我在表之间建立了 1 对多的关系,但是当我尝试在数据透视表中使用它时它失败了。我收到通常的黄色消息,说它可能缺乏关系。当我让它尝试检测一个时,它无法找到任何可能的东西,当我检查现有的时,我的就在那里并
我有一个带有标题列(在许多其他列中)的数据列表,并且我有一个 Power BI 参数,例如,值为“a、b、c”。我想要做的是遍历参数的值并删除以这些字符开头的所有行。 例如: Title a b c
有没有什么方法可以将 Power BI 报表部署到 Power BI 报表服务器,而无需手动复制这些文件,将它们上传到服务器,最后逐个报表更改每个报表的数据源连接信息,这在每个报表中都不实用客户网站。
我的数据源中有一个表,该表是从数据库加载的,其中包含带有日期的列。我需要从此列中获取最小值和最大值,并使用值作为参数在 Power BI 中创建另一个(计算的)表。请问我该怎么做?我尝试使用像 Sta
我正在用 Java 编写一个数学应用程序,它使用 Javascript 进行脚本/输入。我希望能够输入 x^2 并让 Java 在发送到 JavaScript 解析器之前将其替换为 pow(x,2)。
有人要求我将 Sharepoint 上的 Excel 在线电子表格中的数据提取到 Power BI 中以创建仪表板 - 没问题,对吧?好吧,“数据点”之一实际上是指示状态的单元格的填充颜色。我进行了一
在 Power Automate 中,我正在调用一个返回此 JSON 的 API: { "status":"200", "Suburbs":[ { "
我想从此页面(和类似页面)抓取数据:https://cereals.ahdb.org.uk/market-data-centre/historical-data/feed-ingredients.as
如何使用 PowerQuery 访问与 PowerQuery 关联的元数据?当将鼠标悬停在右侧“工作簿查询”列表中的查询上时,会显示此数据,显示“上次刷新”等字段。 应用程序:我有一个 Excel 工
我是一名优秀的程序员,十分优秀!