多线程异步调用问题
请改造以下类,使之在多线程异步调用时,而不会出数据覆盖,即并发冲突pulbic class Math {
private static int result = 0;
public static int sum(int a, int b)
{ result = a + b; return result; }
}
请教大神 怎么做 --------------------编程问答-------------------- 什么意思? --------------------编程问答--------------------
pulbic class Math {
private static int result = 0;
public static int sum(int a, int b)
{ return a + b; }
}
这样就可以了, --------------------编程问答-------------------- 妈呀,这题在考 栈 和 堆 在多线程时候的访问特性 --------------------编程问答-------------------- 使用synchronized关键字
--------------------编程问答--------------------
public synchronized static int sum(int a, int b) {
result = a + b;
return result;
}
这样的确不会出现问题。。。。。
补充:Java , Java SE