一、synchronized
基本概念:对象锁synchronized(object){….}用法
在以上的代码块中只能由一个线程执行!!!
wait()、notify()是用在这个代码块当中的。wait()可以使当前线程A马上失去对象锁并且沉睡,直到对象调用notify()唤醒该线程。此时持有对象锁的线程B会先行执行完毕,然后再将对象锁交给线程A继续执行。
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
下面我们开始对Person进行访问,我们想这样一个情景。有两个子线程AB,在A执行到一定条件下,让A暂停,开始执行B,B全部执行完毕,A再继续。。我们可以用一下代码来加以实现
final Person person = new Person("张三",23);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (person){
try {
for (int i = 0 ; i<10 ; i++) {
Thread.sleep(1000);
Log.e(person.getName(),"我是A"+i);
if (i==5){
person.wait();// 此处将A 暂停
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"Thread1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (person){
try {
for ( int i = 0 ; i < 10 ; i++ ){
Thread.sleep(1000);
Log.e(person.getName(),"我是B--"+i);
if (i==5){
person.notify();// 此处重新激活A
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"Thread2");
t1.start();
t2.start();
线程A和线程B是并发执行的。线程A每隔1s打印一条日志。当循环到第6次的时候 wait()放弃对象锁并沉睡。此时线程B获得对象锁开始执行。执行完全部10次循环。notify()唤醒线程A。
01-13 15:45:18.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A0
01-13 15:45:19.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A1
01-13 15:45:20.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A2
01-13 15:45:21.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A3
01-13 15:45:22.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A4
01-13 15:45:23.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A5
01-13 15:45:24.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--0
01-13 15:45:25.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--1
01-13 15:45:26.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--2
01-13 15:45:27.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--3
01-13 15:45:28.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--4
01-13 15:45:29.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--5
01-13 15:45:30.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--6
01-13 15:45:31.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--7
01-13 15:45:32.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--8
01-13 15:45:33.335 15435-15907/com.huaxinzhi.usingthreadpool E/张三: 我是B--9
01-13 15:45:34.335 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A6
01-13 15:45:35.345 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A7
01-13 15:45:36.345 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A8
01-13 15:45:37.345 15435-15906/com.huaxinzhi.usingthreadpool E/张三: 我是A9
二、lock
Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,因为Lock可以锁定任意一段代码:
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内。
三、ReadWriteLock
如果说这就是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
这样,写入和写入互斥,读取和写入互斥,但是读取和读取不互斥,提高了代码效率。