我将使用一个静态变量的多线程。这个变量在线程运行时会被更新。更新过程将通过updateModel
函数完成。这里是我的代码片段我的功能需要同步吗?
public static int total = 0;
...
run(){
// there will be calculation before update value ex: int ext = this.total/10
updateModel();
}
synchronized void updatedModel(){
this.total += 1;
}
updateModel是否需要同步?
您是否真的必须在线程中更新总数? – hasan83
是的,对于你们两个人 –
我建议你改用AtomicInteger。这种方式不需要同步。 – krems