互斥锁是JDK1.5的新特性,它可以实现和synchronize一样的作用,但更加强大。
public class Demo3_ReentrantLock {
public static void main(String[] args) {
Printer3 p = new Printer3();
new Thread(){
public void run() {
while(true){
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
public void run() {
while(true){
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread(){
public void run() {
while(true){
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer3{
private ReentrantLock r = new ReentrantLock();
private Condition c1 = r.newCondition();
private Condition c2 = r.newCondition();
private Condition c3 = r.newCondition();
private int flag = 1;
public void print1() throws InterruptedException{
r.lock(); // 获取锁
if(flag != 1){
c1.await(); // 当前线程等待
}
System.out.print("互");
System.out.print("斥");
System.out.print("锁");
System.out.println();
flag = 2;
c2.signal(); // 唤醒c2
}
public void print2() throws InterruptedException{
r.lock();
if(flag != 2){
c2.await();
}
System.out.print("r");
System.out.print("e");
System.out.print("e");
System.out.print("n");
System.out.print("t");
System.out.print("r");
System.out.print("a");
System.out.print("n");
System.out.print("t");
System.out.println();
flag = 3;
c3.signal();
}
public void print3() throws InterruptedException{
r.lock();
if(flag != 3){
c3.await();
}
System.out.print("L");
System.out.print("o");
System.out.print("c");
System.out.print("k");
System.out.println();
flag = 1;
c1.signal();
}
}
虽然用synchronize也可以实现线程之间的通信(即让线程按照希望的顺序执行),但是用ReentrantLock跟简单,可以唤醒指定线程,而用synchronize需要将所有线程唤醒在逐个进行判断,上面这段代码实现的是交替输出:
也可以用synchronize实现:
public class Demo2_NotifyAll {
public static void main(String[] args) {
Printer2 p = new Printer2();
new Thread () {
public void run(){
while(true){
try {
p.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread () {
public void run(){
while(true){
try {
p.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread () {
public void run(){
while(true){
try {
p.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Printer2{
private int flag = 1;
public void print1() throws InterruptedException{
synchronized(this) {
while(flag != 1){
this.wait();
}
System.out.print("互");
System.out.print("斥");
System.out.print("锁");
System.out.println();
flag = 2;
this.notifyAll(); // 唤醒所有等待线程
}
}
public void print2() throws InterruptedException{
synchronized(this){
while(flag != 2){
this.wait();
}
System.out.print("r");
System.out.print("e");
System.out.print("e");
System.out.print("n");
System.out.print("t");
System.out.print("r");
System.out.print("a");
System.out.print("n");
System.out.print("t");
System.out.println();
flag = 3;
this.notifyAll();
}
}
public void print3() throws InterruptedException{
synchronized(this){
while(flag != 3){
this.wait();
}
System.out.print("L");
System.out.print("o");
System.out.print("c");
System.out.print("k");
System.out.println();
flag = 1;
this.notifyAll();
}
}
}