这是自JDK5.0开始提供的Java并发包里面的类
下面是关于Java并发锁的测试代码
package com.jadyer.thread.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* LockTest
* @author 宏宇
* @create Mar 1, 2012 7:12:04 PM
*/
public class LockTest {
public static void main(String[] args) {
FoodCenter fc = new FoodCenter();
new Thread(new ThreadDog(fc, "It is dog`s food")).start();
new Thread(new ThreadPig(fc, "It is pig`s food")).start();
}
}
class ThreadDog implements Runnable{
private FoodCenter fc;
private String foodname;
public ThreadDog(FoodCenter fc, String foodname){
this.fc = fc;
this.foodname = foodname;
}
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
fc.getFood(foodname);
}
}
}
class ThreadPig implements Runnable{
private FoodCenter fc;
private String foodname;
public ThreadPig(FoodCenter fc, String foodname){
this.fc = fc;
this.foodname = foodname;
}
@Override
public void run() {
while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
fc.getFood(foodname);
}
}
}
/**
* @see =========================================================================================
* @see java.util.concurrent是JDK5.0开始提供的Java并发包,其子包locks是专门用于处理线程锁的
* @see java.util.concurrent.locks.Lock较synchronized关键字比,有一个最大的区别,那就是
* @see Lock可以比synchronized更细粒度的控制锁,体现在try{...}finally{mylock.unlock();}
* @see =========================================================================================
* @see Lock有点像公共厕所的门闩,只有正在使用厕所的人,把门闩打开出来了,其它人才可以使用公厕
* @see Lock比传统线程模型中的synchronized方式更加面向对象。与生活中的锁类似,锁本身也应该是一个对象
* @see 两个线程执行的代码片段要实现同步互斥的效果,那么这两个线程所使用的必须是同一个Lock对象
* @see 并且,锁应该位于待操作的资源类的内部方法中,而非线程代码中
* @see =========================================================================================
*/
class FoodCenter {
Lock mylock = new ReentrantLock(); //创建一个锁
public void getFood(String name) {
int len = name.length();
mylock.lock(); //上锁
try {
for (int i = 0; i < len; i++) {
System.out.print(name.charAt(i));
}
System.out.println();
} finally {
mylock.unlock(); //解锁
}
}
}
下面是关于Java读写锁的测试代码
package com.jadyer.thread.lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Java读写锁
* @see 锁分为读锁和写锁。多个读锁不互斥。读锁与写锁互斥。写锁与写锁互斥。这是由JVM自己控制的
* @see 如果代码只读数据,可以很多人同时读,但不能同时写,那就上读锁
* @see 如果代码修改数据,并且不允许同时读,只能一个人写,那就上写锁
* @see 总之:读的时候上读锁,写的时候上写锁
*/
public class ReadWriteLockDemo{
private Object myData = null;//共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据
ReadWriteLock rwl = new ReentrantReadWriteLock();
public void get() {
rwl.readLock().lock(); //读锁
try {
System.out.println(Thread.currentThread().getName() + " be ready to read data");
Thread.sleep((long) (Math.random() * 1000));
System.out.println(Thread.currentThread().getName() + " have read data : " + myData);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rwl.readLock().unlock();
}
}
public void put(Object data) {
rwl.writeLock().lock(); //写锁
try {
System.out.println(Thread.currentThread().getName() + " be ready to write data");
Thread.sleep((long) (Math.random() * 1000));
this.myData = data;
System.out.println(Thread.currentThread().getName() + " have write data : " + myData);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rwl.writeLock().unlock();
}
}
}
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接: http://blog.csdn.net/hopezhangbo/article/details/7383799