1 单例模型,对资源进行共享。 类如ListTest,用Test类进行测试。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ListTest {
private static Lock lock;
private static ReadWriteLock rwLock;
private static ListTest single=null;
private static int money;
//静态工厂方法
public static ListTest getInstance() {
if (single == null) {
single = new ListTest();
money=10;
lock=new ReentrantLock();
rwLock=new ReentrantReadWriteLock();
}
return single;
}
//输出测试
public void printTest(){
System.out.println("输出为:"+money);
}
public void increaseAmt(int increaseAmt,int circle){
try {
//rwLock.writeLock().lock();
//lock.lock();
System.out.println("");
Thread th=Thread.currentThread();
System.out.println("操作前-------------------------------------------"+th.getName());
money+=increaseAmt;
System.out.println(money+"添加后:"+circle);
System.out.println("操作后-------------------------------------------"+th.getName());
} catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//lock.unlock();
// rwLock.writeLock().unlock();
}
}
}
2 Test类实例
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUM=1000;
Thread[] threads=new Thread[NUM];
final ListTest instance=ListTest.getInstance();
for(int i=0;i<NUM;i++){
if(threads[i]==null){
//System.out.println(i);
final int j=i;
threads[i]=new Thread(new Runnable() {
@Override
public void run(){
instance.increaseAmt(10,j);
}
});
threads[i].start();
}
}
instance.printTest();
}
}
实例分析:
如果不加锁,则执行为乱的,输出为下:
操作前-------------------------------------------Thread-0
操作前-------------------------------------------Thread-1
操作前-------------------------------------------Thread-2
操作前-------------------------------------------Thread-3
操作前-------------------------------------------Thread-4
50添加后:4
输出为:40
操作前-------------------------------------------Thread-7
70添加后:7
40添加后:2
操作后-------------------------------------------Thread-2
30添加后:0
操作后-------------------------------------------Thread-0
20添加后:1
操作前-------------------------------------------Thread-6
操作前-------------------------------------------Thread-5
90添加后:5
操作后-------------------------------------------Thread-5
操作后-------------------------------------------Thread-7
操作后-------------------------------------------Thread-4
60添加后:3
操作前-------------------------------------------Thread-8
操作后-------------------------------------------Thread-3
80添加后:6
操作后-------------------------------------------Thread-1
操作后-------------------------------------------Thread-6
100添加后:8
操作前-------------------------------------------Thread-9
110添加后:9
操作后-------------------------------------------Thread-9
操作后-------------------------------------------Thread-8
添加ReentrantLock锁后,(可轮询的、定时的、可中断 lock() unlock())结合Condition将线程放入相应阻塞队列,比较灵活
public void increaseAmt(int increaseAmt,int circle){
try {
//rwLock.writeLock().lock();
lock.lock();
System.out.println("");
Thread th=Thread.currentThread();
System.out.println("操作前-------------------------------------------"+th.getName());
money+=increaseAmt;
System.out.println(money+"添加后:"+circle);
System.out.println("操作后-------------------------------------------"+th.getName());
} catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
lock.unlock();
// rwLock.writeLock().unlock();
}
}
或者ReentrantReadWriteLock (读写锁、写写锁互斥,读读锁共享,常用于缓存如terrocotta)
public void increaseAmt(int increaseAmt,int circle){
try {
rwLock.writeLock().lock();
lock.lock();
System.out.println("");
Thread th=Thread.currentThread();
System.out.println("操作前-------------------------------------------"+th.getName());
money+=increaseAmt;
System.out.println(money+"添加后:"+circle);
System.out.println("操作后-------------------------------------------"+th.getName());
} catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
lock.unlock();
rwLock.writeLock().unlock();
}
}
输出正常如下:
操作前-------------------------------------------Thread-0
20添加后:0
操作后-------------------------------------------Thread-0
操作前-------------------------------------------Thread-1
30添加后:1
操作后-------------------------------------------Thread-1
输出为:30
操作前-------------------------------------------Thread-8
40添加后:8
操作后-------------------------------------------Thread-8
操作前-------------------------------------------Thread-2
50添加后:2
操作后-------------------------------------------Thread-2
操作前-------------------------------------------Thread-3
60添加后:3
操作后-------------------------------------------Thread-3
操作前-------------------------------------------Thread-4
70添加后:4
操作后-------------------------------------------Thread-4
操作前-------------------------------------------Thread-5
80添加后:5
操作后-------------------------------------------Thread-5
操作前-------------------------------------------Thread-6
90添加后:6
操作后-------------------------------------------Thread-6
操作前-------------------------------------------Thread-7
100添加后:7
操作后-------------------------------------------Thread-7
操作前-------------------------------------------Thread-9
110添加后:9
操作后-------------------------------------------Thread-9