从主线程开始,我启动了20个线程。我每个线程(例如)执行5次迭代。如何计算周期值。换句话说,我希望被连续编号。计算发生在线程内部。 (它知道它的编号。)我稍后希望能够说出诸如“ 127号循环中发生错误”之类的内容。
可以这么说,我正在寻找计算公式。
非常感谢您!
Example:
Thread: 0 Iteration 0 = Cycle 1
Thread: 0 Iteration 1 = Cycle 2
Thread: 0 Iteration 2 = Cycle 3
Thread: 0 Iteration 3 = Cycle 4
Thread: 0 Iteration 4 = Cycle 5
Thread: 1 Iteration 0 = Cycle 6
Thread: 1 Iteration 1 = Cycle 7
Thread: 1 Iteration 2 = Cycle 8
Thread: 1 Iteration 3 = Cycle 9
Thread: 1 Iteration 4 = Cycle 10
Thread: 2 Iteration 0 = Cycle 11
...
Thread: 9 Iteration 0 = Cycle 46
Thread: 9 Iteration 1 = Cycle 47
Thread: 9 Iteration 2 = Cycle 48
Thread: 9 Iteration 3 = Cycle 49
Thread: 9 Iteration 4 = Cycle 50
答案 0 :(得分:1)
使用AtomicInteger。这是int的线程安全实现,因此每个线程都可以访问它并计算迭代次数。