- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道这两种在 Java 7 中使用多线程执行任务的方法之间是否有任何区别:
简要要求 - 我必须同时处理 200 条记录,并且每条记录只能处理一次。
方法一:
public class Counter {
private static int count = -1;
private static final Object lock = new Object();
public static int getCount() {
synchronized (lock) {
return ++count;
}
}
}
public class Task implements Runnable {
@Override
public void run() {
int count = 0;
while((count = Counter.getCount()) < 200) {
System.out.println(Thread.currentThread().getName() + " " + count);
// process record # count
}
}
}
public class Tester {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
service.execute(new Task());
}
service.shutdown();
service.awaitTermination(5, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
方法二:
public class Task2 implements Runnable {
private int i = 0;
public Task2(int i) {
super();
this.i = i;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " " + i);
// process record # i
}
}
public class Tester2 {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 200; i++) {
service.execute(new Task2(i));
}
service.shutdown();
service.awaitTermination(5, TimeUnit.MINUTES);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
两者都运行良好并产生相同的输出并且花费的时间几乎相同。然而,示例代码中采用的 200 个数字在实际场景中将以十亿为单位。请建议在内存和 CPU 等方面哪个更好。
谢谢。
编辑 - 根据 Jimmy T. 的建议添加方法 3。
方法 3:
public class Tester3 {
public static void main(String[] args) throws InterruptedException {
int processedRecords = 0;
int totalRecords = 200;
int recordsPerThread = 10;
boolean continueProcess = true;
ExecutorService service = Executors.newFixedThreadPool(10);
while(continueProcess) {
int startIndex = processedRecords;
int endIndex = startIndex + recordsPerThread - 1;
if (endIndex >= totalRecords - 1) {
endIndex = totalRecords - 1;
continueProcess = false;
}
processedRecords = processedRecords + recordsPerThread;
service.submit(new Task3(startIndex, endIndex));
}
service.shutdown();
service.awaitTermination(5, TimeUnit.MINUTES);
}
}
public class Task3 implements Runnable {
private int startIndex = 0;
private int endIndex = 0;
public Task3(int startIndex, int endIndex) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
System.out.println("processing records from " + startIndex + " to " + endIndex);
}
}
最佳答案
Brief requirement - I have to process 200 records, simultaneously, and each record should be processed once only.
However 200 number taken in example code will be in billions in actual scenario.
一如既往,答案是“视情况而定”。除非您的任务非常快,否则同步或对象创建的开销应该无关紧要。请注意,ExecutorService
也必须在内部进行一些同步。
您可以通过使用 AtomicInteger
计数器来避免同步并简化您的代码。但我很可能会选择一个 Queue
和几个 Runnable
来轮询
,因为它很简单并且给你很大的灵 active :
PriorityQueue
您可以预填充队列或使用由生产者填充的 BlockingQueue
。队列的内容可以是记录索引或(可能更好)记录本身。
每个任务轮询队列并在队列为空时终止。
public class Task3 implements Runnable {
@Override public void run() {
while (true) {
final Integer i = queue.poll();
if (i==null) break;
System.out.println(Thread.currentThread().getName() + " " + i);
// process record # i
}
}
}
您只创建有限数量的它们来让您的所有核心保持忙碌。
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) service.execute(new Task3());
关于java - 哪种方法更好——多线程示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25534751/
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Sample data for IPv6? 除了 wireshark 在其网站上提供的内容之外,是否有可以下
我正在寻找可以集成到现有应用程序中并使用多拖放功能的示例或任何现成的解决方案。我在互联网上找到的大多数解决方案在将多个项目从 ListBox 等控件拖放到另一个 ListBox 时效果不佳。谁能指出我
我是 GATE Embedded 的新手,我尝试了简单的示例并得到了 NoClassDefFoundError。首先我会解释我尝试了什么 在 D:\project\gate-7.0 中下载并提取 Ga
是否有像 Eclipse 中的 SWT 示例那样的多合一 JFace 控件示例?搜索(在 stackoverflow.com 上使用谷歌搜索和搜索)对我没有帮助。 如果它是一个独立的应用程序或 ecl
我找不到任何可以清楚地解释如何通过 .net API(特别是 c#)使用谷歌计算引擎的内容。有没有人可以指点我什么? 附言我知道 API 引用 ( https://developers.google.
最近在做公司的一个项目时,客户需要我们定时获取他们矩阵系统的数据。在与客户进行对接时,提到他们的接口使用的目前不常用的BASIC 认证。天呢,它好不安全,容易被不法人监听,咋还在使用呀。但是没办法呀,
最近在做公司的一个项目时,客户需要我们定时获取他们矩阵系统的数据。在与客户进行对接时,提到他们的接口使用的目前不常用的BASIC 认证。天呢,它好不安全,容易被不法人监听,咋还在使用呀。但是没办法呀,
我正在尝试为我的应用程序设计配置文件格式并选择了 YAML。但是,这(显然)意味着我需要能够定义、解析和验证正确的 YAML 语法! 在配置文件中,必须有一个名为 widgets 的集合/序列。 .这
你能给我一个使用 pysmb 库连接到一些 samba 服务器的例子吗?我读过有类 smb.SMBConnection.SMBConnection(用户名、密码、my_name、remote_name
linux服务器默认通过22端口用ssh协议登录,这种不安全。今天想做限制,即允许部分来源ip连接服务器。 案例目标:通过iptables规则限制对linux服务器的登录。 处理方法:编
我一直在寻找任何 PostProjectAnalysisTask 工作代码示例,但没有看。 This页面指出 HipChat plugin使用这个钩子(Hook),但在我看来它仍然使用遗留的 Po
我发现了 GWT 的 CustomScrollPanel 以及如何自定义滚动条,但我找不到任何示例或如何设置它。是否有任何示例显示正在使用的自定义滚动条? 最佳答案 这是自定义 native 滚动条的
我正在尝试开发一个 Backbone Marionette 应用程序,我需要知道如何以最佳方式执行 CRUD(创建、读取、更新和销毁)操作。我找不到任何解释这一点的资源(仅适用于 Backbone)。
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 去年关闭。 Improve this
我需要一个提交多个单独请求的 django 表单,如果没有大量定制,我找不到如何做到这一点的示例。即,假设有一个汽车维修店使用的表格。该表格将列出商店能够进行的所有可能的维修,并且用户将选择他们想要进
我有一个 Multi-Tenancy 应用程序。然而,这个相同的应用程序有 liquibase。我需要在我的所有数据源中运行 liquibase,但是我不能使用这个 Bean。 我的应用程序.yml
我了解有关单元测试的一般思想,并已在系统中发生复杂交互的场景中使用它,但我仍然对所有这些原则结合在一起有疑问。 我们被警告不要测试框架或数据库。好的 UI 设计不适合非人工测试。 MVC 框架不包括一
我正在使用 docjure并且它的 select-columns 函数需要一个列映射。我想获取所有列而无需手动指定。 如何将以下内容生成为惰性无限向量序列 [:A :B :C :D :E ... :A
$condition使用说明和 $param在 findByAttributes在 Yii 在大多数情况下,这就是我使用 findByAttributes 的方式 Person::model()->f
我在 Ubuntu 11.10 上安装了 qtcreator sudo apt-get install qtcreator 安装的版本有:QT Creator 2.2.1、QT 4.7.3 当我启动
我是一名优秀的程序员,十分优秀!