多线程异步调用问题

yanhai775 2013-11-19 09:52:48
请改造以下类,使之在多线程异步调用时,而不会出数据覆盖,即并发冲突
pulbic class Math {
private static int result = 0;
public static int sum(int a, int b)
{ result = a + b; return result;   }
}
请教大神 怎么做
...全文
267 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dong Guo 2013-11-25
  • 打赏
  • 举报
回复
引用 2 楼 spiniper 的回复:
pulbic class Math { 
private static int result = 0; 
public static int sum(int a, int b)
{ return a + b;   } 
}
这样就可以了,
这样的确不会出现问题。。。。。
Dong Guo 2013-11-25
  • 打赏
  • 举报
回复
使用synchronized关键字

public synchronized static int sum(int a, int b) {
        result = a + b;
		
	return result;
}
nj_dobetter 2013-11-23
  • 打赏
  • 举报
回复
妈呀,这题在考 栈 和 堆 在多线程时候的访问特性
树成 2013-11-19
  • 打赏
  • 举报
回复
pulbic class Math { 
private static int result = 0; 
public static int sum(int a, int b)
{ return a + b;   } 
}
这样就可以了,
ganshenml 2013-11-19
  • 打赏
  • 举报
回复
什么意思?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧