[英]Java threads relation to CPU cores
Let's say i have CPU with 2 cores. 假设我有2个核心的CPU。 If i will run background processing service with Executors.newFixedThreadPool(4) threads am i correct that: 如果我将使用Executors.newFixedThreadPool(4)线程运行后台处理服务,我是否正确:
Example 例
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Launcher {
public static void main(String[] args) throws Exception {
final Stats stats = new Stats();
final Random key = new Random();
ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
String keyValue = String.valueOf(key.nextInt(10));
int value = stats.inc(keyValue);
System.out.println("[A] Key " + keyValue + " was incremented to " + value);
try {
TimeUnit.MILLISECONDS.sleep(1500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
});
service.submit(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
int[] values = new int[10];
for (int i = 0; i< 10; i++) {
values[i] = stats.get(String.valueOf(i));
}
System.out.println("[B] " + Arrays.toString(values));
try {
TimeUnit.MILLISECONDS.sleep(1500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
});
}
static class Stats {
private final Map<String, Number> statistics = new HashMap<String, Number>();
public int inc(String key) {
if (!statistics.containsKey(key)) {
synchronized (statistics) {
statistics.put(key, new AtomicInteger(0));
}
}
return ((AtomicInteger) statistics.get(key)).getAndIncrement();
}
public int get(String key) {
if (!statistics.containsKey(key)) {
return 0;
}
return statistics.get(key).intValue();
}
}
}
Could you point me to some valuable documentation of low level management of multithreaded code in java? 您能否指出一些有价值的Java低级多线程代码管理文档?
Guys i really understand that we should not rely on specific architecture/CPU/ etc. I'm just curious if probability of described points bigger than 0 :) 伙计们,我真的很明白我们不应该依赖于特定的体系结构/ CPU /等等。我很好奇所描述的点的概率是否大于0 :)
Thx in advance 提前Thx
You shouldn't make any assumptions about threads seeing values modified by other threads unless you synchronize on the access or make the variables volatile. 除非在访问上同步或使变量可变,否则您不应对线程看到其他线程修改的值做任何假设。
Any other behaviour is unreliable and subject to change. 任何其他行为都是不可靠的,并且可能会发生变化。
Remember that Java is running on the JVM, not directly on your processor, and has license to make a LOT of optimisations to your running code. 请记住,Java是在JVM上运行的,而不是直接在您的处理器上运行的,并且具有对运行代码进行大量优化的许可。 So while a lot of the behaviour carries over you cannot rely upon it. 因此,尽管许多行为都可以延续,但您不能依靠它。 Especially since as soon as you run on different architecture or under different conditions the exact same bytecode may be optimised differently. 特别是因为一旦您在不同的体系结构上运行或在不同的条件下运行,则可能完全不同地优化了完全相同的字节码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.