当前位置: 移动技术网 > IT编程>开发语言>Java > Java线程之锁对象Lock-同步问题更完美的处理方式代码实例

Java线程之锁对象Lock-同步问题更完美的处理方式代码实例

2019年07月19日  | 移动技术网IT编程  | 我要评论

lock是java.util.concurrent.locks包下的接口,lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我们拿java线程之线程同步synchronized和volatile详解中的一个例子简单的实现一下和sychronized一样的效果,代码如下:

public class locktest { 
  public static void main(string[] args) { 
    final outputter1 output = new outputter1(); 
    new thread() { 
      public void run() { 
        output.output("zhangsan"); 
      }; 
    }.start();    
    new thread() { 
      public void run() { 
        output.output("lisi"); 
      }; 
    }.start(); 
  } 
} 
class outputter1 { 
  private lock lock = new reentrantlock();// 锁对象 
  public void output(string name) { 
    // todo 线程输出方法 
    lock.lock();// 得到锁 
    try { 
      for(int i = 0; i < name.length(); i++) { 
        system.out.print(name.charat(i)); 
      } 
    } finally { 
      lock.unlock();// 释放锁 
    } 
  } 
} 

这样就实现了和sychronized一样的同步效果,需要注意的是,用sychronized修饰的方法或者语句块在代码执行完之后锁自动释放,而用lock需要我们手动释放锁,所以为了保证锁最终被释放(发生异常情况),要把互斥区放在try内,释放锁放在finally内。

如果说这就是lock,那么它不能成为同步问题更完美的处理方式,下面要介绍的是读写锁(readwritelock),我们会有一种需求,在对数据进行读写的时候,为了保证数据的一致性和完整性,需要读和写是互斥的,写和写是互斥的,但是读和读是不需要互斥的,这样读和读不互斥性能更高些,来看一下不考虑互斥情况的代码原型:

public class readwritelocktest { 
  public static void main(string[] args) { 
    final data data = new data(); 
    for (int i = 0; i < 3; i++) { 
      new thread(new runnable() { 
        public void run() { 
          for (int j = 0; j < 5; j++) { 
            data.set(new random().nextint(30)); 
          } 
        } 
      }).start(); 
    }     
    for (int i = 0; i < 3; i++) { 
      new thread(new runnable() { 
        public void run() { 
          for (int j = 0; j < 5; j++) { 
            data.get(); 
          } 
        } 
      }).start(); 
    } 
  } 
} 
class data {   
  private int data;// 共享数据   
  public void set(int data) { 
    system.out.println(thread.currentthread().getname() + "准备写入数据"); 
    try { 
      thread.sleep(20); 
    } catch (interruptedexception e) { 
      e.printstacktrace(); 
    } 
    this.data = data; 
    system.out.println(thread.currentthread().getname() + "写入" + this.data); 
  }   
  public void get() { 
    system.out.println(thread.currentthread().getname() + "准备读取数据"); 
    try { 
      thread.sleep(20); 
    } catch (interruptedexception e) { 
      e.printstacktrace(); 
    } 
    system.out.println(thread.currentthread().getname() + "读取" + this.data); 
  } 
} 

部分输出结果:

thread-1准备写入数据 
thread-3准备读取数据 
thread-2准备写入数据 
thread-0准备写入数据 
thread-4准备读取数据 
thread-5准备读取数据 
thread-2写入12 
thread-4读取12 
thread-5读取5 
thread-1写入12 

我们要实现写入和写入互斥,读取和写入互斥,读取和读取互斥,在set和get方法加入sychronized修饰符:

public synchronized void set(int data) {...}   
public synchronized void get() {...} 

部分输出结果:

thread-0准备写入数据 
thread-0写入9 
thread-5准备读取数据 
thread-5读取9 
thread-5准备读取数据 
thread-5读取9 
thread-5准备读取数据 
thread-5读取9 
thread-5准备读取数据 
thread-5读取9 

我们发现,虽然写入和写入互斥了,读取和写入也互斥了,但是读取和读取之间也互斥了,不能并发执行,效率较低,用读写锁实现代码如下:

class data {   
  private int data;// 共享数据 
  private readwritelock rwl = new reentrantreadwritelock();   
  public void set(int data) { 
    rwl.writelock().lock();// 取到写锁 
    try { 
      system.out.println(thread.currentthread().getname() + "准备写入数据"); 
      try { 
        thread.sleep(20); 
      } catch (interruptedexception e) { 
        e.printstacktrace(); 
      } 
      this.data = data; 
      system.out.println(thread.currentthread().getname() + "写入" + this.data); 
    } finally { 
      rwl.writelock().unlock();// 释放写锁 
    } 
  }   
  public void get() { 
    rwl.readlock().lock();// 取到读锁 
    try { 
      system.out.println(thread.currentthread().getname() + "准备读取数据"); 
      try { 
        thread.sleep(20); 
      } catch (interruptedexception e) { 
        e.printstacktrace(); 
      } 
      system.out.println(thread.currentthread().getname() + "读取" + this.data); 
    } finally { 
      rwl.readlock().unlock();// 释放读锁 
    } 
  } 
} 

部分输出结果:

thread-4准备读取数据 
thread-3准备读取数据 
thread-5准备读取数据 
thread-5读取18 
thread-4读取18 
thread-3读取18 
thread-2准备写入数据 
thread-2写入6 
thread-2准备写入数据 
thread-2写入10 
thread-1准备写入数据 
thread-1写入22 
thread-5准备读取数据 

从结果可以看出实现了我们的需求,这只是锁的基本用法,锁的机制还需要继续深入学习。

总结

以上就是本文关于java线程之锁对象lock-同步问题更完美的处理方式代码实例的全部内容,希望对大家有所帮助,感兴趣的朋友可以继续参阅本站:javaapi的使用方法详解、、java编程接口调用的作用及代码分享等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网