- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想保留一个已执行线程的计数器,以便在我正在执行的相同线程中使用。
这里的问题是,尽管计数器增加了,但计数器却不均匀地增加,从控制台输出中我得到了这个(我有一个用ExecutorService执行5个线程的for循环):
This is a test. N:3
This is a test. N:4
This is a test. N:4
This is a test. N:4
This is a test. N:4
1,2,3,4,5
,而是得到了
3,4,4,4,4
。
for (int i = 0; i < 5; i++)
{
Thread thread;
thread = new Thread()
{
public void run()
{
System.out.println("This is test. N: "+aldo );
//In here there is much more stuff, saying it because it might slow down the execution (if that is the culprit?)
return;
}
};
threadList.add(thread);
}
//later
for (int i = 0; i < threadList.size(); i++)
{
executor.execute(threadList.get(i));
aldo = aldo + 1;
}
executor.shutdown();
try
{
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
}
catch (InterruptedException e)
{
}
aldo
计数器(我认为还有一些其他列表)在代码中丢失了(它们非常简单)。
最佳答案
我所知道的最好方法是通过使用传入数字的构造函数创建自定义线程类。包含数字的变量随后可用于以后任何所需的日志记录。这是我想出的代码。
public static void main(String[] args) {
class NumberedThread implements Runnable {
private final int number;
public NumberedThread(int number) {
this.number = number;
}
@Override
public void run() {
System.out.println("This is test. N: " + number);
}
}
List<Thread> threadList = new ArrayList<>();
for (int i = 1; i < 6; i++) threadList.add(new Thread(new NumberedThread(i)));
ExecutorService executor = Executors.newFixedThreadPool(10);;
for (Thread thread : threadList) executor.execute(thread);
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
}
catch (InterruptedException ignored) { }
}
关于java - 与ExecutorService保持对抗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48551291/
如果我有一个像盒子一样的 div,我用 10% 的不透明度让它变得非常性感。我该如何应对它,因为 div 中的所有内容也都具有不透明度。假设我有一个带有 1px 边框和文本的框 (div),在其上设置
我知道 AngularJS 会运行某些代码两次,有时甚至更多,例如 $watch 事件,不断检查模型状态等。 但是我的代码: function MyController($scope, User, l
目前,我们在对我们的 cassandra 数据库使用 apache spark 时挑战我们的架构,因为我们遇到了非常糟糕的读取性能。 spark & cassandra 发生的硬件是一个云服务器,16
所以最近我遇到了以下问题:我必须创建一个处理请求的服务器,以便在主进程使用这些值时更新某些值。 所以这里,服务器处理函数是在子进程中,我想停止的时候都无法停止。 为了测试在 threading.Thr
准备好的语句不允许参数化表名。为了确保无法插入任何代码,我想使用 ctype_alnum 来验证进入数据库模块的所有表名(删除下划线后),以保护应用程序免受其他部分错误的影响。 function in
经典算法书籍(TAOCP、CLR)(以及不太经典的书籍,例如 fxtbook)充满了命令式 算法。这对于实现严重基于数组的算法最为明显,例如组合生成(算法中同时使用数组索引和数组值)或联合查找算法。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
这个问题已经有答案了: Apply a Greasemonkey/Tampermonkey userscript to an iframe (6 个回答) 已关闭 5 年前。 我想知道是否可以让 Gr
SELECT p.id FROM produkty p, przyporzadkowania pr, stany_magazynowe, gk_grupy_produkty INNER JOIN su
我想将 Azure ACS 添加到我的一个站点,但所有功能都出现问题。我有一个 MVC 应用程序,它使用全局过滤器为请求的生命周期设置自定义 IPrincipal。 WIF 的“添加 STS 引用”方
我想将 Azure ACS 添加到我的一个站点,但所有功能都出现问题。我有一个 MVC 应用程序,它使用全局过滤器为请求的生命周期设置自定义 IPrincipal。 WIF 的“添加 STS 引用”方
Google 的 YouTube Analytics API 仅基于 Oauth2。我正在使用以下测试脚本来查看是否可以获得访问权限: import os import google.oauth2.c
随着 Visual Studio 2008 的发布,Microsoft 为 JavaScript 实现了代码完成和 IntelliSense 支持。 不幸的是,对 JavaScript 的支持充其量是
我在准备语句方面有更多经验,我知道它们非常适合抵御 SQL 注入(inject)攻击。 我想知道 pl/pgsql 的 format/USING 和 quote_literal/quote_nulla
我是一名优秀的程序员,十分优秀!