Java多线程atomic原子变量

J2SE 5.0提供了一组atomic class来帮助我们简化同步处理。基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增、减、赋值(更新)操作. 比如对于++运算符AtomicInteger可以将它持有的integer 能够atomic 地递增。在需要访问两个或两个以上 atomic变量的程序代码(或者是对单一的atomic变量执行两个或两个以上的操作)通常都需要被synchronize以便两者的操作能够被当作是一个atomic的单元。 

对array atomic变量来说,一次只有一个索引变量可以变动,并没有功能可以对整个array做atomic化的变动。 

关于Atomic的几个方法 
getAndSet() : 设置新值,返回旧值. 
compareAndSet(expectedValue, newValue) : 如果当前值(current value)等于期待的值(expectedValue), 则原子地更新指定值为新值(newValue), 如果更新成功,返回true, 否则返回false, 换句话可以这样说: 将原子变量设置为新的值, 但是如果从我上次看到的这个变量之后到现在被其他线程修改了(和我期望看到的值不符), 那么更新失败 

从effective java (2)中拿来的一个关于AtomicReference的一个例子: 
Java代码 
Java代码  复制代码  收藏代码
  1. public class AtomicTest {      
  2.     private int x, y;      
  3.      
  4.     private enum State {      
  5.         NEW, INITIALIZING, INITIALIZED      
  6.     };      
  7.      
  8.     private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);      
  9.           
  10.     public AtomicTest() {      
  11.     }      
  12.           
  13.     public AtomicTest(int x, int y) {      
  14.         initialize(x, y);      
  15.     }      
  16.      
  17.     private void initialize(int x, int y) {      
  18.         if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {      
  19.             throw new IllegalStateException("initialize is error");      
  20.         }      
  21.         this.x = x;      
  22.         this.y = y;      
  23.         init.set(State.INITIALIZED);      
  24.     }      
  25.      
  26.     public int getX() {      
  27.         checkInit();      
  28.         return x;      
  29.     }      
  30.      
  31.     public int getY() {      
  32.         checkInit();      
  33.         return y;      
  34.     }      
  35.           
  36.     private void checkInit() {      
  37.         if (init.get() == State.INITIALIZED) {      
  38.             throw new IllegalStateException("uninitialized");      
  39.         }      
  40.     }      
  41.           
  42. }     
  43.   
  44. public class AtomicTest {   
  45.     private int x, y;   
  46.   
  47.     private enum State {   
  48.         NEW, INITIALIZING, INITIALIZED   
  49.     };   
  50.   
  51.     private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);   
  52.        
  53.     public AtomicTest() {   
  54.     }   
  55.        
  56.     public AtomicTest(int x, int y) {   
  57.         initialize(x, y);   
  58.     }   
  59.   
  60.     private void initialize(int x, int y) {   
  61.         if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {   
  62.             throw new IllegalStateException("initialize is error");   
  63.         }   
  64.         this.x = x;   
  65.         this.y = y;   
  66.         init.set(State.INITIALIZED);   
  67.     }   
  68.   
  69.     public int getX() {   
  70.         checkInit();   
  71.         return x;   
  72.     }   
  73.   
  74.     public int getY() {   
  75.         checkInit();   
  76.         return y;   
  77.     }   
  78.        
  79.     private void checkInit() {   
  80.         if (init.get() == State.INITIALIZED) {   
  81.             throw new IllegalStateException("uninitialized");   
  82.         }   
  83.     }   
  84.        
  85. }  
[java]  view plain  copy
  1. public class AtomicTest {     
  2.     private int x, y;     
  3.     
  4.     private enum State {     
  5.         NEW, INITIALIZING, INITIALIZED     
  6.     };     
  7.     
  8.     private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);     
  9.          
  10.     public AtomicTest() {     
  11.     }     
  12.          
  13.     public AtomicTest(int x, int y) {     
  14.         initialize(x, y);     
  15.     }     
  16.     
  17.     private void initialize(int x, int y) {     
  18.         if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {     
  19.             throw new IllegalStateException("initialize is error");     
  20.         }     
  21.         this.x = x;     
  22.         this.y = y;     
  23.         init.set(State.INITIALIZED);     
  24.     }     
  25.     
  26.     public int getX() {     
  27.         checkInit();     
  28.         return x;     
  29.     }     
  30.     
  31.     public int getY() {     
  32.         checkInit();     
  33.         return y;     
  34.     }     
  35.          
  36.     private void checkInit() {     
  37.         if (init.get() == State.INITIALIZED) {     
  38.             throw new IllegalStateException("uninitialized");     
  39.         }     
  40.     }     
  41.          
  42. }    
  43.   
  44. public class AtomicTest {  
  45.     private int x, y;  
  46.   
  47.     private enum State {  
  48.         NEW, INITIALIZING, INITIALIZED  
  49.     };  
  50.   
  51.     private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);  
  52.       
  53.     public AtomicTest() {  
  54.     }  
  55.       
  56.     public AtomicTest(int x, int y) {  
  57.         initialize(x, y);  
  58.     }  
  59.   
  60.     private void initialize(int x, int y) {  
  61.         if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {  
  62.             throw new IllegalStateException("initialize is error");  
  63.         }  
  64.         this.x = x;  
  65.         this.y = y;  
  66.         init.set(State.INITIALIZED);  
  67.     }  
  68.   
  69.     public int getX() {  
  70.         checkInit();  
  71.         return x;  
  72.     }  
  73.   
  74.     public int getY() {  
  75.         checkInit();  
  76.         return y;  
  77.     }  
  78.       
  79.     private void checkInit() {  
  80.         if (init.get() == State.INITIALIZED) {  
  81.             throw new IllegalStateException("uninitialized");  
  82.         }  
  83.     }  
  84.       
  85. }  


上面的例子比较容易懂, 不过貌似没什么价值, 而在实际的应用中, 我们一般采用下面的方式来使用atomic class: 
Java代码 
Java代码  复制代码  收藏代码
  1. public class CounterTest {      
  2.     AtomicInteger counter = new AtomicInteger(0);      
  3.      
  4.     public int count() {      
  5.         int result;      
  6.         boolean flag;      
  7.         do {      
  8.             result = counter.get();      
  9.             // 断点      
  10.             // 单线程下, compareAndSet返回永远为true,      
  11.             // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,      
  12.             // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值      
  13.             flag = counter.compareAndSet(result, result + 1);      
  14.         } while (!flag);      
  15.      
  16.         return result;      
  17.     }      
  18.      
  19.     public static void main(String[] args) {      
  20.         final CounterTest c = new CounterTest();      
  21.         new Thread() {      
  22.             @Override     
  23.             public void run() {      
  24.                 c.count();      
  25.             }      
  26.         }.start();      
  27.      
  28.         new Thread() {      
  29.             @Override     
  30.             public void run() {      
  31.                 c.count();      
  32.             }      
  33.         }.start();      
  34.      
  35.         new Thread() {      
  36.             @Override     
  37.             public void run() {      
  38.                 c.count();      
  39.             }      
  40.         }.start();      
  41.     }      
  42. }     
  43.   
  44. public class CounterTest {   
  45.     AtomicInteger counter = new AtomicInteger(0);   
  46.   
  47.     public int count() {   
  48.         int result;   
  49.         boolean flag;   
  50.         do {   
  51.             result = counter.get();   
  52.             // 断点   
  53.             // 单线程下, compareAndSet返回永远为true,   
  54.             // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,   
  55.             // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值   
  56.             flag = counter.compareAndSet(result, result + 1);   
  57.         } while (!flag);   
  58.   
  59.         return result;   
  60.     }   
  61.   
  62.     public static void main(String[] args) {   
  63.         final CounterTest c = new CounterTest();   
  64.         new Thread() {   
  65.             @Override  
  66.             public void run() {   
  67.                 c.count();   
  68.             }   
  69.         }.start();   
  70.   
  71.         new Thread() {   
  72.             @Override  
  73.             public void run() {   
  74.                 c.count();   
  75.             }   
  76.         }.start();   
  77.   
  78.         new Thread() {   
  79.             @Override  
  80.             public void run() {   
  81.                 c.count();   
  82.             }   
  83.         }.start();   
  84.     }   
  85. }  
[java]  view plain  copy
  1. public class CounterTest {     
  2.     AtomicInteger counter = new AtomicInteger(0);     
  3.     
  4.     public int count() {     
  5.         int result;     
  6.         boolean flag;     
  7.         do {     
  8.             result = counter.get();     
  9.             // 断点     
  10.             // 单线程下, compareAndSet返回永远为true,     
  11.             // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,     
  12.             // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值     
  13.             flag = counter.compareAndSet(result, result + 1);     
  14.         } while (!flag);     
  15.     
  16.         return result;     
  17.     }     
  18.     
  19.     public static void main(String[] args) {     
  20.         final CounterTest c = new CounterTest();     
  21.         new Thread() {     
  22.             @Override    
  23.             public void run() {     
  24.                 c.count();     
  25.             }     
  26.         }.start();     
  27.     
  28.         new Thread() {     
  29.             @Override    
  30.             public void run() {     
  31.                 c.count();     
  32.             }     
  33.         }.start();     
  34.     
  35.         new Thread() {     
  36.             @Override    
  37.             public void run() {     
  38.                 c.count();     
  39.             }     
  40.         }.start();     
  41.     }     
  42. }    
  43.   
  44. public class CounterTest {  
  45.     AtomicInteger counter = new AtomicInteger(0);  
  46.   
  47.     public int count() {  
  48.         int result;  
  49.         boolean flag;  
  50.         do {  
  51.             result = counter.get();  
  52.             // 断点  
  53.             // 单线程下, compareAndSet返回永远为true,  
  54.             // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,  
  55.             // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值  
  56.             flag = counter.compareAndSet(result, result + 1);  
  57.         } while (!flag);  
  58.   
  59.         return result;  
  60.     }  
  61.   
  62.     public static void main(String[] args) {  
  63.         final CounterTest c = new CounterTest();  
  64.         new Thread() {  
  65.             @Override  
  66.             public void run() {  
  67.                 c.count();  
  68.             }  
  69.         }.start();  
  70.   
  71.         new Thread() {  
  72.             @Override  
  73.             public void run() {  
  74.                 c.count();  
  75.             }  
  76.         }.start();  
  77.   
  78.         new Thread() {  
  79.             @Override  
  80.             public void run() {  
  81.                 c.count();  
  82.             }  
  83.         }.start();  
  84.     }  
  85. }  

类似i++这样的"读-改-写"复合操作(在一个操作序列中, 后一个操作依赖前一次操作的结果), 在多线程并发处理的时候会出现问题, 因为可能一个线程修改了变量, 而另一个线程没有察觉到这样变化, 当使用原子变量之后, 则将一系列的复合操作合并为一个原子操作,从而避免这种问题, i++=>i.incrementAndGet() 

原子变量只能保证对一个变量的操作是原子的, 如果有多个原子变量之间存在依赖的复合操作, 也不可能是安全的, 另外一种情况是要将更多的复合操作作为一个原子操作, 则需要使用synchronized将要作为原子操作的语句包围起来. 因为涉及到可变的共享变量(类实例成员变量)才会涉及到同步, 否则不必使用synchronized。


来源:http://blog.csdn.net/kaiwii/article/details/6753795

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值