1.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();// 释放锁
}
}
}
2.Condition的应用
public class ConditionTest {
public static void main(String[] args) {
final Business business = new Business();
new Thread(new Runnable() {
@Override
public void run() {
threadExecute(business, "sub");
}
}).start();
threadExecute(business, "main");
}
public static void threadExecute(Business business, String threadType) {
for (int i = 0; i < 10; i++) {
try {
if ("main".equals(threadType)) {
business.main(i);
} else {
business.sub(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Business {
private boolean bool = true;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public/* synchronized */void main(int loop) throws InterruptedException {
lock.lock();
try {
while (bool) {
condition.await();// this.wait();
}
for (int i = 0; i < 10; i++) {
System.out.println("main thread seq of " + i + ", loop of "
+ loop);
}
bool = true;
condition.signal();// this.notify();
} finally {
lock.unlock();
}
}
public/* synchronized */void sub(int loop) throws InterruptedException {
lock.lock();
try {
while (!bool) {
condition.await();// this.wait();
}
for (int i = 0; i < 10; i++) {
System.out.println("sub thread seq of " + i + ", loop of "
+ loop);
}
bool = false;
condition.signal();// this.notify();
} finally {
lock.unlock();
}
}
}
3.读写锁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;// 共享数据
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();// 释放写锁
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
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();// 释放读锁
}
}
}
4.环形缓存区简单实现
public 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;// 如果写索引写到队列的最后一个位置了,那么置为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;// 如果读索引读到队列的最后一个位置了,那么置为0
}
--count;// 个数--
notFull.signal();// 唤醒写线程
return x;
} finally {
lock.unlock();
}
}
}