• <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 逛奔的蝸牛 閱讀(985) 評論(0)  編輯 收藏 引用 所屬分類: Java
            久久一区二区三区99| 久久天天躁狠狠躁夜夜躁2014| 久久久久亚洲AV片无码下载蜜桃| 精品久久久久久久久午夜福利| 狠狠久久综合| 亚洲AV日韩AV永久无码久久| 久久电影网2021| 精品久久久无码21p发布| 国产福利电影一区二区三区久久老子无码午夜伦不 | 婷婷国产天堂久久综合五月| 久久久精品2019免费观看| 狠狠精品干练久久久无码中文字幕| 久久久久久伊人高潮影院| 久久精品国产91久久麻豆自制| 欧洲性大片xxxxx久久久| 欧美亚洲国产精品久久蜜芽| 国产69精品久久久久久人妻精品| 精品无码久久久久久国产| 久久久久无码精品国产| 久久久久亚洲国产| 久久国产乱子伦精品免费午夜| 国产精品久久久久天天影视| 久久狠狠爱亚洲综合影院| 久久久久久青草大香综合精品| 国产成人久久精品激情| 五月丁香综合激情六月久久| 亚洲v国产v天堂a无码久久| 久久艹国产| 久久伊人亚洲AV无码网站| 国产巨作麻豆欧美亚洲综合久久 | 久久久无码精品亚洲日韩软件| 蜜桃麻豆www久久| 99久久精品日本一区二区免费| 久久人人爽人人人人爽AV | 少妇被又大又粗又爽毛片久久黑人 | 久久香蕉国产线看观看99 | 久久久黄片| 久久精品国产一区二区三区不卡 | 久久精品一本到99热免费| 一本色道久久综合亚洲精品| 无码精品久久久天天影视|