预自增运算符是线程安全的吗?

发布于 2024-12-08 09:38:54 字数 597 浏览 0 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

野却迷人 2024-12-15 09:38:54

不,您应该使用类似于 java.util.concurrent.atomic.AtomicInteger 的东西。查看其 getAndIncrement() 方法。

No, you should be using something like java.util.concurrent.atomic.AtomicInteger. Look at its getAndIncrement() method.

简单气质女生网名 2024-12-15 09:38:54

int 上的预增量不是线程安全,请使用无锁的AtomicInteger

AtomicInteger carsComplete = new AtomicInteger();

//...

switch(carsComplete.incrementAndGet())

顺便说一句,下面的代码不是强> 线程也是安全的。你能告诉我为什么吗?

carsComplete.incrementAndGet();
switch(carsComplete.get())

Pre-increment on int is not thread safe, use AtomicInteger which is lock-free:

AtomicInteger carsComplete = new AtomicInteger();

//...

switch(carsComplete.incrementAndGet())

BTW the code below is not thread safe as well. Can you tell why?

carsComplete.incrementAndGet();
switch(carsComplete.get())
樱娆 2024-12-15 09:38:54

++ 运算符不是原子的。看看这里http://madbean.com/2003/mb2003-44/
对于原子操作,您可以使用 AtomicInteger

AtomicInteger atomicInteger = new java.util.concurrent.atomic.AtomicInteger(0)

,每次您想要递增时,您都可以调用 atomicInteger.incrementAndGet() 方法,该方法返回一个原始 int。 0 是原子整数的默认初始值。

++ operator is not atomic. Look at here http://madbean.com/2003/mb2003-44/.
For atomic operations you can use AtomicInteger

AtomicInteger atomicInteger = new java.util.concurrent.atomic.AtomicInteger(0)

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.

最佳男配角 2024-12-15 09:38:54

与 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 is load/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 construct AtomicInteger or you could synchronize the whole method

止于盛夏 2024-12-15 09:38:54

问题是“预增量运算符线程安全吗?”

答:不,为什么?因为涉及的指令数量。
原子意味着单个操作,这里需要执行加载/添加/存储操作。所以不是原子操作。

  Same for post increment.

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.

  Same for post increment.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文