当前位置:编程学习 > JAVA >>

多线程异步调用问题

请改造以下类,使之在多线程异步调用时,而不会出数据覆盖,即并发冲突 
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;
}
--------------------编程问答--------------------
引用 2 楼 spiniper 的回复:
pulbic class Math { 
private static int result = 0; 
public static int sum(int a, int b)
{ return a + b;   } 
}

这样就可以了,

这样的确不会出现问题。。。。。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,