以鎖的方式循環(huán)執(zhí)行兩個線程,當(dāng)然也可以使用lock.newCondition實現(xiàn)線程的喚醒
Condition con1 = lock.newCondition();
final Lock lock = new ReentrantLock();
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
int i = 0;
lock.lock();
Log.e("xmq", "aaaaaaaaaa");
lock.unlock();
try {
sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread1 = new Thread() {
@Override
public void run() {
while (true) {
int i = 0;
lock.lock();
Log.e("xmq", "bbbbbbbbbbbbb");
lock.unlock();
try {
sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
thread1.start();
ReentrantReadWriteLock 讀寫鎖