多线程-简单的同步

模拟苹果店卖出和买进ipad,刚开始一共有200个ipad1,200个ipad2,两家同时卖。求剩余的ipad1,ipad2

1  定义IPAD 

public class IPad {  
    private long ipad1Number;  
    private long ipad2Number;  
    public IPad(){  
        ipad1Number = 200;  
        ipad2Number = 200;  
    }  
    public long getIpad1Number(){  
        return ipad1Number;  
    }  
    public long getIpad2Number(){  
        return ipad2Number;  
    }  
    /** 
     *  卖出ipad1
     * @param number 
     * @return 
     */  
    public synchronized boolean sellIpad1(int number){  
        if(number <= ipad1Number){  
            ipad1Number -= number;  
            return true;  
        }  
        return false;  
    }  
    /** 
     * 卖出ipad2
     * @param number 
     * @return 
     */  
    public synchronized boolean sellIpad2(int number){  
        if(number <= ipad2Number){  
            ipad2Number -= number;  
            return true;  
        }  
        return false;  
    }  
    /** 
     * 买进ipad1
     * @param number 
     * @return true 
     */  
    public synchronized boolean returnIpad2(int number){  
        ipad2Number += number;  
        return true;  
    }  
    /** 
     * 买进ipad2
     * @param number 
     * @return true 
     */  
    public synchronized boolean returnIpad1(int number){  
        ipad1Number += number;  
        return true;  
    }  
}

 

2 定义2家苹果店,以及苹果店的行为

 

public class IPadShop1 implements Runnable {  
    private IPad ipad;  
    public IPadShop1(IPad ipad){  
        this.ipad = ipad;  
    }  
    /** 
     * 苹果店1卖出和买进分析
     */  
    public void run() { 
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.sellIpad1(15));  
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.sellIpad1(196));//false  
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.sellIpad1(19));  
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.returnIpad1(1));  
        // 一共200个,第一次15可以卖出,第二次196失败,因为还剩余185个,第三次卖19个成功,此时剩余(185-19),第四次买进1个
        // 所以卖出个数为 15+19-1=33
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.sellIpad2(150));  
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.sellIpad2(96));//false  
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.sellIpad2(3));  
        System.out.println("IPadShop1 Selling ipad1 15 number is "+ipad.returnIpad2(3)); 
        // 第一次卖出150 ,第二次不够,第三四和第四次相当于没有任何变动,所以苹果店1卖出150个
    }  
}

 

public class IPadShop2 implements Runnable {  
    private IPad ipad;  
    public IPadShop2(IPad ipad){  
        this.ipad = ipad;  
    }  
    /** 
     * 苹果店2卖出和买进分析
     */  
    public void run() {  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.sellIpad1(23));  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.sellIpad1(16));//false  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.sellIpad1(19));  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.returnIpad1(1));  
        // shop1 卖出33个 shop2 卖出 57个,两家一共卖出90个,所以还剩余110个
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.sellIpad2(50));  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.sellIpad2(96));//false  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.sellIpad2(13));//false  
        System.out.println("IPadShop2 Selling ipad1 15 number is "+ipad.returnIpad2(3)); 
        // 苹果店1卖出150个,还剩余50个,这第一次刚好卖完 没有了就卖不出去,最后添加了3个,所以还剩余3个
    }  
}

 3  调用

public class SellingIPad {  
    public static void main(String[] args) {  
        IPad ipad = new IPad();// 只有这么多手机,ipad1 200个,ipad2 也是200个
        IPadShop1 shop1 = new IPadShop1(ipad);  
        Thread t1 = new Thread(shop1, "苹果店1");  
        IPadShop2 shop2 = new IPadShop2(ipad);  
        Thread t2 = new Thread(shop2, "苹果店2");  
        t1.start();  
        t2.start();  
       try {  
            t1.join();//  
            t2.join();//   
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }
        System.out.println("ipad 1 剩余 : "+ ipad.getIpad1Number());  
        System.out.println("ipad 2 剩余 : "+ ipad.getIpad2Number());  
    }  
}

 

 这种同步方式性能比较差,换成锁的方式:

只需要修改Ipad对象

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class IPad {  
	private final Lock queueLock; 
	  
    private long ipad1Number;  
    private long ipad2Number;  
    public IPad(){ 
    	queueLock =new ReentrantLock();
        ipad1Number = 200;  
        ipad2Number = 200;  
    }  
    public long getIpad1Number(){  
        return ipad1Number;  
    }  
    public long getIpad2Number(){  
        return ipad2Number;  
    }  
    /** 
     *  卖出ipad1
     * @param number 
     * @return 
     */  
    public  boolean sellIpad1(int number){ 
    	  
        if(number <= ipad1Number){  
        	queueLock.lock(); 
            ipad1Number -= number; 
            queueLock.unlock();
            return true;  
        }
        return false;  
    }  
    /** 
     * 卖出ipad2
     * @param number 
     * @return 
     */  
    public  boolean sellIpad2(int number){  
        if(number <= ipad2Number){ 
        	queueLock.lock(); 
            ipad2Number -= number; 
            queueLock.unlock(); 
            return true;  
        }  
        return false;  
    }  
    /** 
     * 买进ipad1
     * @param number 
     * @return true 
     */  
    public  boolean returnIpad2(int number){ 
    	queueLock.lock(); 
        ipad2Number += number;  
        queueLock.unlock(); 
        return true;  
    }  
    /** 
     * 买进ipad2
     * @param number 
     * @return true 
     */  
    public  boolean returnIpad1(int number){
    	queueLock.lock(); 
        ipad1Number += number;  
        queueLock.unlock(); 
        return true;  
    }  
}

 

 

你可能感兴趣的:(多线程-简单的同步)