Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

当有关键字synchronized时,为何IDEA建议用Collections.unmodifiableSet?

有关键字synchronized用在set上时
image.png

idea总是建议如下:

image.png

为什么要这样?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

报错内容是说“用方法参数或者局部变量做synchronized很难保证逻辑正确,建议换class或者field做synchronized”
第二段代码里换成一些其他操作一样可以消除这个警告,估计是idea已经彻底无法理解这个操作是要干啥了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...