- 使用condition实现同步
public class ConditionCommunication {
public static void main(String[] args) {
final Bussiness bussiness = new Bussiness();
new Thread(
new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 50; i++) {
bussiness.sub(i);
}
}
}
).start();
for (int i = 0; i <= 50; i++) {
bussiness.main(i);
}
}
static class Bussiness {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private Boolean shouldSub = true;
public void main(int i) {
lock.lock();
try {
while (shouldSub) {//if
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 100; j++) {
System.out.println("main thread sequence of " + j + " of loop " + i);
}
shouldSub = true;
condition.signal();
} finally {
lock.unlock();
}
}
public void sub(int i) {
lock.lock();
try {
while (!shouldSub) {//用if可能会发生虚假唤醒
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 10; j++) {
System.out.println("sub thread sequence of " + j + " of loop " + i);
}
shouldSub = false;
condition.signal();
} finally {
lock.unlock();
}
}
}
}
(2)
假设我们有一个有限的缓冲区,它支持put和take方法。 如果在一个空的缓冲区尝试一个take ,则线程将阻塞直到一个项目可用; 如果put试图在一个完整的缓冲区,那么线程将阻塞,直到空间变得可用。 我们希望在单独的等待集中等待put线程和take线程,以便我们可以在缓冲区中的项目或空间可用的时候使用仅通知单个线程的优化。 这可以使用两个Condition实例来实现。
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock(); try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally { lock.unlock(); }
}
public Object take() throws InterruptedException {
lock.lock(); try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally { lock.unlock(); }
}
} ( ArrayBlockingQueue类提供此功能,因此没有理由实现此示例使用类。)
- 老大循环5次,老二循环6次,老三循环7次,如此循环50次;(用三个condition)
**
* 三个conditon进行通讯:
* 老大循环5次,老二循环6次,老三循环7次,如此循环50次
*/
public class ThreeConditionCommunication {
public static void main(String[] args) {
final Bussiness bussiness = new Bussiness();
new Thread(
new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 50; i++) {
bussiness.sub1(i);
}
}
}
).start();
new Thread(
new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 50; i++) {
bussiness.sub2(i);
}
}
}
).start();
for (int i = 0; i <= 50; i++) {
bussiness.main(i);
}
}
static class Bussiness {
private Lock lock = new ReentrantLock();
private Condition shouldMainCondition = lock.newCondition();
private Condition shouldSub1Condition = lock.newCondition();
private Condition shouldSub2Condition = lock.newCondition();
private Boolean shouldMain = true;//也可以用数字,这个时候一个数字就可以
private Boolean shouldSub1 = false;
private Boolean shouldSub2 = false;
public void main(int i) {
lock.lock();
try {
while (!shouldMain) {
try {
shouldMainCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 5; j++) {
System.out.println("main thread sequence of " + j + " of loop " + i);
}
shouldMain = false;
shouldSub1=true;
shouldSub1Condition.signal();
} finally {
lock.unlock();
}
}
public void sub1(int i) {
lock.lock();
try {
while (!shouldSub1) {
try {
shouldSub1Condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 6; j++) {
System.out.println("sub1 thread sequence of " + j + " of loop " + i);
}
shouldSub1 = false;
shouldSub2 = true;
shouldSub2Condition.signal();
} finally {
lock.unlock();
}
}
public void sub2(int i) {
lock.lock();
try {
while (!shouldSub2) {
try {
shouldSub2Condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < 7; j++) {
System.out.println("sub2 thread sequence of " + j + " of loop " + i);
}
shouldSub2 = false;
shouldMain = true;
shouldMainCondition.signal();
} finally {
lock.unlock();
}
}
}
}
如有疑问,请发邮件:1176306419@qq.com
github:??https://github.com/wangrui0/