Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,当然也能实现sychronized一样的效果,代码如下:
package org.thread; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Output { private Lock lock = new ReentrantLock(); public void output(String name) { try { lock.lock(); for (int i = 0; i < name.length(); i++) { System.out.print(name.charAt(i)); } } catch (Exception e) { } finally { lock.unlock(); } } }
现在看看
package org.thread; public class Outputer { public synchronized void input(String name){ for(int i =0;i<name.length();i++){ System.out.print(name.charAt(i)); } } }
这两个方法其实都是为了实现数据同步
package org.thread; public class LockTest { public static void main(String[] args) { final Outputer outputer = new Outputer(); final Output output = new Output(); Runnable runnable1 = new Runnable() { public void run() { outputer.input("Heinrich"); } }; Runnable runnable2 = new Runnable() { @Override public void run() { outputer.input("AmiLoveMe"); } }; Runnable runnable3 = new Runnable() { public void run() { output.output("Heinrich"); } }; Runnable runnable4 = new Runnable() { @Override public void run() { output.output("AmiLoveMe"); } }; new Thread(runnable2).start(); new Thread(runnable1).start(); new Thread(runnable3).start(); new Thread(runnable4).start(); } }
看看控制台的信息是不是我们想要的,结果肯定是的,反正我觉得是
AmiLoveMeHeinrichHeinrichAmiLoveMe 是我要的结果,有序,数据一致
这样就实现了和sychronized一样的同步效果,需要注意的是,用sychronized修饰的方法或者语句块在代码执行完之后锁自动释放,而是用Lock需要我们手动释放锁,所以为了保证锁最终被释放(发生异常情况),要把互斥区放在try内,释放锁放在finally内。
如果说这就是Lock,那么它不能成为同步问题更完美的处理方式,下面要介绍的是读写锁(ReadWriteLock),我们会有一种需求,在对数据进行读写的时候,为了保证数据的一致性和完整性,需要读和写是互斥的,写和写是互斥的,但是读和读是不需要互斥的,这样读和读不互斥性能更高些,来看一下不考虑互斥情况的代码原型:
现在看代码:没有读写锁的
package org.thread; public class Data { private Integer data; public void get() { System.out.println(Thread.currentThread().getName()+"准备读取数据"); try { Thread.sleep(4000); } catch (Exception e) { } System.out.println(Thread.currentThread().getName()+"读取"+this.data); } public void set(Integer data) { System.out.println(Thread.currentThread().getName()+"准备写入数据"); try { Thread.sleep(4000); } catch (Exception e) { } this.data = data; System.out.println(Thread.currentThread().getName()+"写入"+this.data); } }
package org.thread; import java.util.Random; public class DataThread { public static void main(String[] args) { final Data data = new Data(); for (int i = 0; i < 10; i++) { Runnable runnable1 = new Runnable() { @Override public void run() { data.set(new Random().nextInt(100)); } }; new Thread(runnable1).start(); } for (int i = 0; i < 10; i++) { Runnable runnable2 = new Runnable() { @Override public void run() { data.get(); } }; new Thread(runnable2).start(); } } }
看看控制台
Thread-0准备写入数据 Thread-8准备写入数据 Thread-5准备写入数据 Thread-4准备写入数据 Thread-3准备写入数据 Thread-2准备写入数据 Thread-7准备写入数据 Thread-1准备写入数据 Thread-6准备写入数据 Thread-9准备写入数据 Thread-10准备读取数据 Thread-12准备读取数据 Thread-11准备读取数据 Thread-14准备读取数据 Thread-15准备读取数据 Thread-16准备读取数据 Thread-13准备读取数据 Thread-17准备读取数据 Thread-18准备读取数据 Thread-19准备读取数据 Thread-7写入28 Thread-3写入56 Thread-8写入83 Thread-2写入88 Thread-9写入83 Thread-5写入88 Thread-0写入84 Thread-6写入28 Thread-1写入28 Thread-4写入28 Thread-19读取56 Thread-12读取56 Thread-14读取56 Thread-13读取56 Thread-16读取56 Thread-10读取56 Thread-11读取56 Thread-15读取56 Thread-17读取56 Thread-18读取56
我们要实现写入和写入互斥,读取和写入互斥,读取和读取互斥,在set和get方法加入
synchronized修饰符
package org.thread; public class Data { private Integer data; public synchronized void get() { System.out.println(Thread.currentThread().getName()+"准备读取数据"); try { Thread.sleep(4000); } catch (Exception e) { } System.out.println(Thread.currentThread().getName()+"读取"+this.data); } public synchronized void set(Integer data) { System.out.println(Thread.currentThread().getName()+"准备写入数据"); try { Thread.sleep(4000); } catch (Exception e) { } this.data = data; System.out.println(Thread.currentThread().getName()+"写入"+this.data); } }
Thread-0准备写入数据 Thread-0写入54 Thread-19准备读取数据 Thread-19读取54 Thread-18准备读取数据 Thread-18读取54 Thread-17准备读取数据 Thread-17读取54 Thread-16准备读取数据 Thread-16读取54 Thread-15准备读取数据 Thread-15读取54 Thread-14准备读取数据 Thread-14读取54 Thread-13准备读取数据 Thread-13读取54 Thread-12准备读取数据 Thread-12读取54 Thread-11准备读取数据 Thread-11读取54 Thread-10准备读取数据 Thread-10读取54 Thread-9准备写入数据 Thread-9写入42 Thread-8准备写入数据 Thread-8写入86 Thread-1准备写入数据 Thread-1写入30 Thread-2准备写入数据 Thread-2写入3 Thread-4准备写入数据 Thread-4写入81 Thread-3准备写入数据 Thread-3写入72 Thread-5准备写入数据 Thread-5写入15 Thread-6准备写入数据 Thread-6写入16 Thread-7准备写入数据 Thread-7写入33
我们发现,虽然写入和写入互斥了,读取和写入也互斥了,但是读取和读取之间也互斥了,不能并发执行,效率较低,用读写锁实现代码如下:
package org.thread; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class DataSyn { private Integer data; private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public void get() { System.out.println(Thread.currentThread().getName() + "准备读取数据"); readWriteLock.readLock().lock();; try { Thread.sleep(2000); System.out.println(Thread.currentThread().getName() + "正在读取数据" + this.data); } catch (Exception e) { } finally { readWriteLock.readLock().unlock(); } } public void set(Integer data) { System.out.println(Thread.currentThread().getName() + "准备写入数据"); readWriteLock.writeLock().lock();; try { this.data = data; System.out.println(Thread.currentThread().getName()+"正在写入"+data); } catch (Exception e) { e.printStackTrace(); }finally{ readWriteLock.writeLock().unlock(); } } }
Thread-5准备写入数据 Thread-5正在写入85 Thread-1准备写入数据 Thread-1正在写入74 Thread-6准备写入数据 Thread-6正在写入28 Thread-0准备写入数据 Thread-0正在写入93 Thread-3准备写入数据 Thread-4准备写入数据 Thread-7准备写入数据 Thread-8准备写入数据 Thread-3正在写入75 Thread-2准备写入数据 Thread-2正在写入75 Thread-4正在写入70 Thread-7正在写入65 Thread-9准备写入数据 Thread-8正在写入92 Thread-9正在写入88
这个是不是效率更好了,当然咯,要不然说这么多干什么