线程同步之模拟银行取款

  线程同步有三种办法,同步块,同步方法,可重入锁ReentrantLock。用实例说明:

  1.可重入锁ReentrantLock。  

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


public class WithDrawMoney implements Runnable{
    static int total;
    static int left;
    Random random = new Random();
    private final ReentrantLock lock = new ReentrantLock();

    public WithDrawMoney(int n) {
        super();
        this.total = n;
        left = total;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        lock.lock();
        if (left > 0){
            int take_out = random.nextInt(100);
            if (take_out > left){
                take_out = left;
            }
            System.out.printf("线程%s,目前余额为%d元,取走了%d元,剩余%d元\n",Thread.currentThread().getName(),left,take_out,left-take_out);
            left -= take_out;
        }
        lock.unlock();
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WithDrawMoney bank = new WithDrawMoney(2000);
        for (int i=0; i<10; i++){
            Thread thread = new Thread(bank);
            thread.start();
        }
    }


}

  2.同步方法。

  

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


public class WithDrawMoney implements Runnable{
    static int total;
    static int left;
    Random random = new Random();

    public WithDrawMoney(int n) {
        super();
        this.total = n;
        left = total;
    }

    @Override
    public synchronized void run() {
        // TODO Auto-generated method stub
        if (left > 0){
            int take_out = random.nextInt(100);
            if (take_out > left){
                take_out = left;
            }
            System.out.printf("线程%s,目前余额为%d元,取走了%d元,剩余%d元\n",Thread.currentThread().getName(),left,take_out,left-take_out);
            left -= take_out;
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WithDrawMoney bank = new WithDrawMoney(2000);
        for (int i=0; i<10; i++){
            Thread thread = new Thread(bank);
            thread.start();
        }
    }


}

  3.同步块

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


public class WithDrawMoney implements Runnable{
    static int total;
    static int left;
    Random random = new Random();

    public WithDrawMoney(int n) {
        super();
        this.total = n;
        left = total;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        synchronized(WithDrawMoney.class){
            if (left > 0){
                int take_out = random.nextInt(100);
                if (take_out > left){
                    take_out = left;
                }
                System.out.printf("线程%s,目前余额为%d元,取走了%d元,剩余%d元\n",Thread.currentThread().getName(),left,take_out,left-take_out);
                left -= take_out;
            }
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WithDrawMoney bank = new WithDrawMoney(200);
        for (int i=0; i<10; i++){
            Thread thread = new Thread(bank);
            thread.start();
        }
    }


}

 

posted @ 2017-06-15 10:14  林嘉瑜  阅读(1030)  评论(0编辑  收藏  举报