ReentrantLock实现同步锁
public class MyTask {
private Lock lock = new ReentrantLock();
public void execute() {
lock.lock();
for (int i = 0 ; i < 3 ; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
lock.unlock();
}
}
public class MyThread extends Thread {
private MyTask myTask;
public MyThread(MyTask myTask) {
this.myTask = myTask;
}
@Override
public void run() {
myTask.execute();
}
}
public class MyTest {
public static void main(String[] args) {
MyTask myTask = new MyTask();
MyThread mt1 = new MyThread(myTask);
mt1.setName("mt1");
MyThread mt2 = new MyThread(myTask);
mt2.setName("mt2");
mt1.start();
mt2.start();
}
}
结果:
mt1 0
mt1 1
mt1 2
mt2 0
mt2 1
mt2 2
Condition等待/唤醒
public class MyTask {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void executeWait() {
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " await start");
condition.await();
System.out.println(Thread.currentThread().getName() + " await end");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void executeSignal() {
lock.lock();
System.out.println(Thread.currentThread().getName() + " signal start");
condition.signal();
System.out.println(Thread.currentThread().getName() + " signal end");
lock.unlock();
}
}
public class MyTest {
public static void main(String[] args) {
final MyTask myTask = new MyTask();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
myTask.executeWait();
}
});
t1.setName("t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
myTask.executeSignal();
}
});
t2.setName("t2");
t1.start();
t2.start();
}
}
结果:
t1 await start
t2 signal start
t2 signal end
t1 await end
多个Condition实现部分通知
public class MyTask {
private Lock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
public void executeCondition1() {
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " executeCondition1 start");
condition1.await();
System.out.println(Thread.currentThread().getName() + " executeCondition1 end");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void executeCondition2() {
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + " executeCondition2 start");
condition2.await();
System.out.println(Thread.currentThread().getName() + " executeCondition2 end");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void executeSignal1() {
lock.lock();
System.out.println(Thread.currentThread().getName() + " executeSignal1 start");
condition1.signalAll();
System.out.println(Thread.currentThread().getName() + " executeSignal1 end");
lock.unlock();
}
public void executeSignal2() {
lock.lock();
System.out.println(Thread.currentThread().getName() + " executeSignal2 start");
condition2.signalAll();
System.out.println(Thread.currentThread().getName() + " executeSignal2 end");
lock.unlock();
}
}
public class MyTest {
public static void main(String[] args) {
final MyTask myTask = new MyTask();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
myTask.executeCondition1();
}
});
t1.setName("t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
myTask.executeCondition2();
}
});
t2.setName("t2");
t1.start();
t2.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myTask.executeSignal1();
}
}
结果:
1.png
网友评论