java - int 是线程安全的吗?
ringa_lee
ringa_lee 2017-04-18 10:54:59
0
3
848

例如在springmvc的controller中

    public class EnchashmentController extends BaseController {
            private int a = 0;
            
            @requestMapping("")
            public ModelAndView add(){
                a++;
                return null;
            }
    }

每一个请求都在这里都执行一次a加一操作,1万次请求(并发)甚至更多次以后,这里输出的a是否就是 10000 或者 100000呢?

ringa_lee
ringa_lee

ringa_lee

reply all(3)
阿神

It should be said that a++ is not thread-safe

小葫芦

AtomicInteger should be the answer you want

阿神

Not safe. . . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template