Java非线程安全类变线程安全类

一般来说非线程安全类是有状态的类,就是有属性的类。先看一个非线程安全类的例子:

Java代码   收藏代码
  1. package info.yiwen.thread.safe;  
  2. /** 
  3.  * 该类是非线程安全类,因为有一个共享变量state。 
  4.  *  
  5.  * 原因:当多个线程使用同一个NotThreadSafe类的一个对象时, 
  6.  * 也会共享该对象的state属性,故是非线程安全的。 
  7.  *  
  8.  * 也因为该类是非线程安全的,所以该类也不能作为单例对象。 
  9.  *  
  10.  * 但是稍加改造也可以成为线程安全的,改造的方法有使用ThreadLocal,使用同步等等 
  11.  *  
  12.  * 项目名称:ThreadSafeClass   
  13.  * 类名称:NotThreadSafe   
  14.  * 类描述:暂无 
  15.  * 创建人:xueshishasha   
  16.  * 创建时间:2012-6-7 下午09:58:13   
  17.  * 修改备注: 
  18.  * @version 
  19.  */  
  20. public class NotThreadSafe {  
  21.     private boolean state;  
  22.   
  23.     public boolean checkState() {  
  24.         return isState();  
  25.     }  
  26.       
  27.     public boolean isState() {  
  28.         return state;  
  29.     }  
  30.   
  31.     public void setState(boolean state) {  
  32.         this.state = state;  
  33.     }  
  34. }  



下面就稍加改造变成线程安全类:
方法1:使用ThreadLocal

Java代码   收藏代码
  1. package info.yiwen.thread.safe;  
  2.   
  3. /** 
  4.  * 该对象是一个线程安全的对象,他虽然有一个state属性,但是是放在ThreadLocal里的, 
  5.  * 也就是说表面上看state属性是属于本类的,其实是属于当前线程的。 
  6.  *  
  7.  * 因为每个线程实际上拥有一个state的副本,因此,state属性是不共享的,除此之外,该类也没有其他的共享的属性, 
  8.  * 因此该类是线程安全的,可以作为单例对象。 
  9.  *  
  10.  *  
  11.  * 项目名称:ThreadSafeClass   
  12.  * 类名称:ThreadSafeWithThreadLocal   
  13.  * 类描述:暂无 
  14.  * 创建人:xueshishasha   
  15.  * 创建时间:2012-6-7 下午10:02:35   
  16.  * 修改备注: 
  17.  * @version 
  18.  */  
  19. public class ThreadSafeWithThreadLocal {  
  20.     private final static ThreadLocal<Boolean> state = new ThreadLocal<Boolean>();  
  21.       
  22.     public ThreadSafeWithThreadLocal(boolean state) {  
  23.         this.state.set(state);  
  24.     }  
  25.       
  26.     public boolean checkState() {  
  27.         return state.get();  
  28.     }  
  29.   
  30.     public boolean getState() {  
  31.         return this.state.get();  
  32.     }  
  33.   
  34.     public void setState(boolean state) {  
  35.         this.state.set(state);  
  36.     }  
  37.       
  38.       
  39. }  


方法2:使用同步关键字synchronized

Java代码   收藏代码
  1. package info.yiwen.thread.safe;  
  2.   
  3.   
  4. /** 
  5.  * 该类也是线程安全的,该类有一个共享属性state,但是对state状态进行写的方法上都用了sysnchronized同步语句 
  6.  *  
  7.  * 项目名称:ThreadSafeClass   
  8.  * 类名称:ThreadSafeWithSynchronized   
  9.  * 类描述:暂无 
  10.  * 创建人:xueshishasha   
  11.  * 创建时间:2012-6-7 下午10:13:50   
  12.  * 修改备注: 
  13.  * @version 
  14.  */  
  15. public class ThreadSafeWithSynchronized {  
  16.       
  17.     private Boolean state;  
  18.       
  19.     public boolean checkState() {  
  20.         return state;  
  21.     }  
  22.   
  23.     public boolean isState() {  
  24.         return state;  
  25.     }  
  26.   
  27.     public void setState(boolean state) {  
  28.         synchronized (this.state) {  
  29.             this.state = state;  
  30.         }  
  31.     }  
  32.       
  33.       
  34. }  


方法3:使用锁

Java代码   收藏代码
  1. package info.yiwen.thread.safe;  
  2.   
  3. import java.util.concurrent.locks.Lock;  
  4. import java.util.concurrent.locks.ReentrantLock;  
  5.   
  6.   
  7. /** 
  8.  * 该类也是线程安全的,该类有一个共享属性state,但是对state状态进行写的方法上都用了lock锁同步 
  9.  *  
  10.  * 项目名称:ThreadSafeClass   
  11.  * 类名称:ThreadSafeWithSynchronized   
  12.  * 类描述:暂无 
  13.  * 创建人:xueshishasha   
  14.  * 创建时间:2012-6-7 下午10:13:50   
  15.  * 修改备注: 
  16.  * @version 
  17.  */  
  18. public class ThreadSafeWithLock {  
  19.       
  20.     private Boolean state;  
  21.       
  22.     private final Lock lock = new ReentrantLock();  
  23.       
  24.     public boolean checkState() {  
  25.         return state;  
  26.     }  
  27.   
  28.     public boolean isState() {  
  29.         return state;  
  30.     }  
  31.   
  32.     public void setState(boolean state) {  
  33.         lock.lock();  
  34.         try {  
  35.             this.state = state;  
  36.         } finally {  
  37.             lock.unlock();  
  38.         }  
  39.     }  
  40.       
  41.       
  42. }  


方法4:使用Atomic类型

Java代码   收藏代码
  1. package info.yiwen.thread.safe;  
  2.   
  3. import java.util.concurrent.atomic.AtomicBoolean;  
  4.   
  5. /** 
  6.  * 该类是线程安全的,该类有一个属性,是用AtomicBoolean实现的, 
  7.  * Atomic类型会维护好线程安全以及他所持有的值的访问权限 
  8.  *  
  9.  * 项目名称:ThreadSafeClass   
  10.  * 类名称:ThreadSafeWithAtomicType   
  11.  * 类描述:暂无 
  12.  * 创建人:xueshishasha   
  13.  * 创建时间:2012-6-7 下午10:59:26   
  14.  * 修改备注: 
  15.  * @version 
  16.  */  
  17.   
  18. public class ThreadSafeWithAtomicType {  
  19.     private AtomicBoolean state = new AtomicBoolean();  
  20.       
  21.     public boolean checkState() {  
  22.         return state.get();  
  23.     }  
  24.   
  25.     public boolean getState() {  
  26.         return state.get();  
  27.     }  
  28.   
  29.     public void setState(boolean state) {  
  30.         this.state.set(state);  
  31.     }  
  32. }  

猜你喜欢

转载自blog.csdn.net/xiaohanzuofengzhou/article/details/80164209