文章来源:http://blog.sina.com.cn/s/blog_6560efc9010185f9.html
Lock是java.util.concurrent.locks包下的接口,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();// 释放锁 -
} -
} - }
- 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;// 共享数据 -
public void set(int data) { -
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); -
} -
public void get() { -
System.out.println(Thread.currentThread().getName() + "准备读取数据"); -
try { -
Thread.sleep(20); -
} catch (InterruptedException e) { -
e.printStackTrace(); -
} -
System.out.println(Thread.currentThread().getName() + "读取" + this.data); -
} - }
- Thread-1准备写入数据
- Thread-3准备读取数据
- Thread-2准备写入数据
- Thread-0准备写入数据
- Thread-4准备读取数据
- Thread-5准备读取数据
- Thread-2写入12
- Thread-4读取12
- Thread-5读取5
- Thread-1写入12
- public
synchronized void set(int data) {...} - public
synchronized void get() {...}
- Thread-0准备写入数据
- Thread-0写入9
- Thread-5准备读取数据
- Thread-5读取9
- Thread-5准备读取数据
- Thread-5读取9
- Thread-5准备读取数据
- Thread-5读取9
- Thread-5准备读取数据
- Thread-5读取9
- 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();// 释放写锁 -
} -
} -
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();// 释放读锁 -
} -
} - }
- Thread-4准备读取数据
- Thread-3准备读取数据
- Thread-5准备读取数据
- Thread-5读取18
- Thread-4读取18
- Thread-3读取18
- Thread-2准备写入数据
- Thread-2写入6
- Thread-2准备写入数据
- Thread-2写入10
- Thread-1准备写入数据
- Thread-1写入22
- Thread-5准备读取数据