• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            import java.util.concurrent.locks.Condition;

            import java.util.concurrent.locks.ReentrantLock;


            public class Test1 {


                public static void main(String[] args) {

                    Bank bank = new Bank(10, 500); // Shared data.

                    for (int i = 0; i < bank.size(); ++i) {

                        BankTransfer bt = new BankTransfer(bank, i, 500);

                        Thread thread = new Thread(bt);

                        thread.start();

                    }

                }

            }


            class Bank {

                private ReentrantLock lock;

                private Condition condition;

                private double[] accounts;


                public Bank(int n, double initialBalance) {

                    accounts = new double[n];

                    for (int i = 0; i < accounts.length; i++) {

                        accounts[i] = initialBalance;

                    }


                    lock = new ReentrantLock();

                    condition = lock.newCondition();

                }


                public int size() {

                    return accounts.length;

                }


                public double getTotalBalance() {

                    double totalBalance = 0;


                    lock.lock();

                    try {

                        for (int i = 0; i < accounts.length; ++i) {

                            totalBalance += accounts[i];

                        }

                    } finally {

                        lock.unlock();

                    }


                    return totalBalance;

                }


                public void transfer(int from, int to, double amount) {

                    lock.lock();

                    try {

                        while (accounts[from] < amount) {

                            condition.await();

                        }

                        System.out.print(Thread.currentThread());

                        accounts[from] -= amount;

                        accounts[to] += amount;

                        System.out.print("\t" + amount + " from " + from + " to " + to);

                        System.out.println("\tTotal balance: " + getTotalBalance());


                        condition.signalAll();

                    } catch (InterruptedException e) {


                    } finally {

                        lock.unlock();

                    }

                }

            }


            class BankTransfer implements Runnable {

                private Bank bank;

                private int fromAcount;

                private double maxAmount;


                public static final int DELAY = 4000;


                public BankTransfer(Bank bank, int fromAcount, double maxAmount) {

                    this.bank = bank;

                    this.fromAcount = fromAcount;

                    this.maxAmount = maxAmount;

                }


                public void run() {

                    try {

                        while (true) {

                            int toAcount = (int) (bank.size() * Math.random());

                            int amount = (int)(maxAmount * Math.random());

                            bank.transfer(fromAcount, toAcount, amount);


                            Thread.sleep((int) (DELAY * Math.random()));

                        }

                    } catch (InterruptedException e) {

                    }

                }

            }

            轉帳是由BankTransfer類去用多線程完成的, Bank類并沒有去實現Runnable接口.


            posted on 2009-06-28 16:16 逛奔的蝸牛 閱讀(979) 評論(0)  編輯 收藏 引用 所屬分類: Java
            99久久国产综合精品女同图片| 久久久亚洲精品蜜桃臀| 亚洲国产成人久久综合野外| 久久九九免费高清视频| 伊人久久大香线蕉AV色婷婷色| 青草国产精品久久久久久| 99精品国产在热久久无毒不卡| 天天久久狠狠色综合| 久久精品人人做人人爽电影 | 精品免费久久久久久久| 久久久久久无码国产精品中文字幕| 亚洲精品无码久久毛片| 久久伊人精品青青草原高清| 怡红院日本一道日本久久 | 国产高潮国产高潮久久久| 久久精品国产亚洲av瑜伽| 亚洲αv久久久噜噜噜噜噜| 亚洲性久久久影院| 久久精品国产精品青草| 99久久99久久| 亚洲午夜久久久久妓女影院| 成人国内精品久久久久影院VR| 久久精品国产网红主播| 99久久精品免费看国产一区二区三区| 久久中文字幕一区二区| 精品国产VA久久久久久久冰| 国产亚洲精久久久久久无码77777| 久久国产精品波多野结衣AV| 国产精品无码久久久久 | 久久无码一区二区三区少妇| 精品久久久久久久久中文字幕| 亚洲αv久久久噜噜噜噜噜| 久久久久久久女国产乱让韩| 久久天天躁狠狠躁夜夜2020老熟妇 | 一级做a爰片久久毛片人呢| 99久久国产主播综合精品| 久久国产乱子伦免费精品| 久久AV高清无码| 久久狠狠高潮亚洲精品| 精品久久久久久中文字幕| 免费精品99久久国产综合精品|