简体   繁体   English

Java - 比较和交换和同步块

[英]Java - Compare and Swap and synchronized Block

public class SimulatedCAS { 
  private int value;

  public synchronized int get() { return value; }

  public synchronized int compareAndSwap(int expectedValue, int newValue) 
  {
  int oldValue = value; 
  if (oldValue == expectedValue) 
      value = newValue;
  return oldValue; 
  }
}

public class CasCounter 
{ 
   private SimulatedCAS value;

   public int getValue() 
   { 
     return value.get();
   }
  
  public int increment() 
  { 
      int value.get(); 
      while (v != value.compareAndSwap(v, v + 1)) 
      {
         v = value.get(); 
      }
  }

}

I refereed a Book "Java Concurrency in Practice"我参考了一本书“Java并发实践”

a Counter must be increased by multiple threads.一个计数器必须由多个线程增加。 I tried using the compare and swap method but at the end it make used of synchronized keyword which might again result in blocking and waiting of threads.我尝试使用比较和交换方法,但最后它使用了同步关键字,这可能再次导致线程阻塞和等待。 using a synchronized block provides me the same performance can anybody state.使用同步块为我提供了与任何人 state 相同的性能。 what is the difference between using compare and swap and synchronized block?使用比较和交换和同步块有什么区别? or any other way to implement compare and swap without using synchronized block.或任何其他方式来实现比较和交换而不使用同步块。

Real Compare and Swap does optimistic locking. Real Compare and Swap 进行乐观锁定。 It changes value and then makes a rollback if something has changed the variable simultaneously.它会更改值,然后如果某些东西同时更改了变量,则进行回滚。 So, if the variable is modified rarely, then CAS performs better, than synchronized.因此,如果变量很少被修改,那么 CAS 的性能比同步的要好。

But if the variable is modified often, then synchronized performs better, because it doesn't allow anything to mess with the variable while it is changed.但是如果变量经常被修改,那么同步的表现会更好,因为它不允许任何东西在变量被改变时弄乱。 And so there's no need to make an expensive rollback.所以没有必要进行昂贵的回滚。

I need to increment counter with multiple threads我需要用多个线程增加计数器

The AtomicInteger class is good for that. AtomicInteger class非常适合。

You can create it with final AtomicInteger i=new AtomicInteger(initial_value);您可以使用final AtomicInteger i=new AtomicInteger(initial_value);创建它Then you can call i.set(new_value) to set its value, and you can call i.get() to get its value, and most importantly for your application, you can call i.incrementAndGet() to atomically increment the value.然后你可以调用i.set(new_value)来设置它的值,你可以调用i.get()来获取它的值,最重要的是你的应用程序,你可以调用i.incrementAndGet()原子地增加值。

If N different threads all call i.incrementAndGet() at "the same time," then如果 N 个不同的线程都“同时”调用i.incrementAndGet() ,那么

  • Each thread is guaranteed to see a different return value, and保证每个线程看到不同的返回值,并且
  • The final value after they're all done is guaranteed to increase by exactly N.全部完成后的最终值保证会增加 N。

The AtomicInteger class has quite a few other methods as well. AtomicInteger class 还有很多其他方法。 Most of them make useful guarantees about what happens when multiple threads access the varaible.它们中的大多数都对当多个线程访问变量时会发生什么做出有用的保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM