预自增运算符是线程安全的吗?
我正在用java编写一个程序,让几辆车互相比赛。每辆车都是一个单独的线程。
当赛车完成比赛时,每辆车都会调用这个方法。我已经在不同的计时器速度下测试了该方法,它似乎工作得很好。但我确实意识到每个线程都在访问变量 carsComplete,有时是在完全相同的时间(至少在 date 命令给我的范围内)。
所以我的问题是:这个方法是线程安全的吗?
public static String completeRace()
{
Date accessDate = new Date();
System.out.println("Cars Complete: " + carsComplete + " Accessed at " + accessDate.toString());
switch(++carsComplete)
{
case 1: return "1st";
case 2: return "2nd";
case 3: return "3rd";
default: return carsComplete + "th";
}
}
I'm making a program in java that races a few cars against each other. Each car is a separate thread.
When cars complete the race, each one all calls this method. I've tested the method at varying timer speeds, and it seems to work fine. But I do realize that each thread is accessing the variable carsComplete, sometimes at the exact same time (at least at the scope the date command is giving me).
So my question is: is this method thread-safe?
public static String completeRace()
{
Date accessDate = new Date();
System.out.println("Cars Complete: " + carsComplete + " Accessed at " + accessDate.toString());
switch(++carsComplete)
{
case 1: return "1st";
case 2: return "2nd";
case 3: return "3rd";
default: return carsComplete + "th";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,您应该使用类似于 java.util.concurrent.atomic.AtomicInteger 的东西。查看其
getAndIncrement()
方法。No, you should be using something like
java.util.concurrent.atomic.AtomicInteger
. Look at itsgetAndIncrement()
method.int
上的预增量不是线程安全,请使用无锁的AtomicInteger
:顺便说一句,下面的代码不是强> 线程也是安全的。你能告诉我为什么吗?
Pre-increment on
int
is not thread safe, useAtomicInteger
which is lock-free:BTW the code below is not thread safe as well. Can you tell why?
++ 运算符不是原子的。看看这里http://madbean.com/2003/mb2003-44/。
对于原子操作,您可以使用
AtomicInteger
,每次您想要递增时,您都可以调用
atomicInteger.incrementAndGet()
方法,该方法返回一个原始 int。 0 是原子整数的默认初始值。++ operator is not atomic. Look at here http://madbean.com/2003/mb2003-44/.
For atomic operations you can use
AtomicInteger
and every time you want to increment you can call
atomicInteger.incrementAndGet()
method which returns a primitive int. 0 is the default initial value for the atomic integer.与 C++ 中相同,运算符
++
不是原子的。它实际上是在后台执行多于 1 条指令(不要被一个简单的
++i
所迷惑;它是load/add/store
)并且由于在没有同步的情况下涉及超过 1 条指令,因此您可能会出现各种交错并产生错误的结果。如果您需要以线程安全的方式增加
carsComplete
,您可以使用 java 的构造AtomicInteger
或者您可以同步整个方法Same as in C++ the operator
++
is not atomic.It is actually more than 1 instruction being executed under the hood (don't be fooled by seeing just a simple
++i
; it isload/add/store
) and since there is more than 1 instruction involved without synchronization you may have various interleavings with wrong results.If you need to incrent the
carsComplete
in a thread-safe manner you can use java's constructAtomicInteger
or you could synchronize the whole method问题是“预增量运算符线程安全吗?”
答:不,为什么?因为涉及的指令数量。
原子意味着单个操作,这里需要执行加载/添加/存储操作。所以不是原子操作。
Question is “is pre increment operator thread safe ?”
Ans : No , Why ? because of number of instructions involved.
Atomic means single operation , here load/add/store operations need to be performed. So not an atomic operation.