java同步实例测试

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值