<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220

    線程3--線程的互斥

    Posted on 2009-06-02 20:14 啥都寫點 閱讀(178) 評論(0)  編輯  收藏 所屬分類: J2SE
    關鍵技術:
    • 在類的方法聲明中使用synchronized關鍵字可以保證在同一時刻只有一個線程能夠進入該方法。
    • synchronized可以出現在代碼塊中,表示同一時刻只有一個線程能夠進入該段代碼。

    package book.thread;

    /**
     * 線程的互斥,主要展示同步方法與非同步方法的區別
     
    */
    public class Synchronized {
        
    /**
         * 帳號類
         
    */
        
    static class Account{
            
    //共享資源, 錢數
            private double money = 1000.0d;
            
    /**
             * 存錢    沒有同步機制
             * 
    @param fFees
             
    */
            
    public void nonSynDeposit(double fFees){
                System.out.println(
    "Account nonSynDeposit begin! money = " + this.money + "; fFees = " + fFees);
                
    //存錢錢先等待300毫秒
                System.out.println("Account nonSynDeposit sleep begin!");
                
    try {
                    Thread.sleep(
    300);
                } 
    catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
    "Account nonSynDeposit sleep end!");
                
    this.money = this.money + fFees;
                System.out.println(
    "Account nonSynDeposit end! money = " + this.money);
            }
            
    /**
             * 取錢    沒有同步機制
             * 
    @param fFees
             
    */
            
    public void nonSynWithdraw(double fFees){
                System.out.println(
    "Account nonSynWithdraw begin! money = " + this.money + "; fFees = " + fFees);
                
    //取錢時先等待400毫秒
                System.out.println("Account nonSynWithdraw sleep begin!");
                
    try {
                    Thread.sleep(
    400);
                } 
    catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
    "Account nonSynWithdraw sleep begin!");
                
    this.money = this.money - fFees;
                System.out.println(
    "Account nonSynWithdraw end! money = " + this.money);
            }
            
    /**
             * 存錢    有同步機制
             * 
    @param fFees
             
    */
            
    public synchronized void synDeposit(double fFees){
                System.out.println(
    "Account synDeposit begin! money = " + this.money + "; fFees = " + fFees);
                
    //存錢錢先等待300毫秒
                System.out.println("Account synDeposit sleep begin!");
                
    try {
                    Thread.sleep(
    300);
                } 
    catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
    "Account synDeposit sleep begin!");
                
    this.money = this.money + fFees;
                System.out.println(
    "Account synDeposit end! money = " + this.money);
            }
            
    /**
             * 取錢    有同步機制
             * 
    @param fFees
             
    */
            
    public synchronized void synWithdraw(double fFees){
                System.out.println(
    "Account synWithdraw begin! money = " + this.money + "; fFees = " + fFees);
                
    //取錢時先等待400毫秒
                System.out.println("Account synWithdraw sleep begin!");
                
    try {
                    Thread.sleep(
    400);
                } 
    catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(
    "Account synWithdraw sleep end!");
                
    this.money = this.money - fFees;
                System.out.println(
    "Account synWithdraw end! money = " + this.money);
            }
        }
        
        
    static class AccessThread extends Thread{
            
    //待訪問的帳號對象
            private Account account = null;
            
    //訪問帳號的方法
            private String method = "";

            
    public AccessThread(Account account, String method){
                
    this.method = method;
                
    this.account = account;
            }
            
    public void run(){
                
    //對不同的方法名參數調用不同的方法
                if (method.equals("nonSynDeposit")){
                    account.nonSynDeposit(
    500.0);
                } 
    else if (method.equals("nonSynWithdraw")){
                    account.nonSynWithdraw(
    200.0);
                } 
    else if (method.equals("synDeposit")){
                    account.synDeposit(
    500.0);
                } 
    else if (method.equals("synWithdraw")){
                    account.synWithdraw(
    200.0);
                }
            }
        }

        
    public static void main(String[] args) {
            
    //待操作的帳號對象,所有操作都針對該帳號
            Account account = new Account();
            
            System.out.println(
    "使用非同步方法時:");
            
    //非同步方法存錢的線程
            Thread threadA = new AccessThread(account, "nonSynDeposit");
            
    //非同步方法取錢的線程
            Thread threadB = new AccessThread(account, "nonSynWithdraw");
            threadA.start();
            threadB.start();
            
    //等待兩線程運行結束
            try {
                threadA.join();
                threadB.join();
            } 
    catch (InterruptedException e) {
                e.printStackTrace();
            }
            
    //下面測試同步方法
            System.out.println();
            account 
    = new Account();
            System.out.println(
    "使用同步方法時:");
            threadA 
    = new AccessThread(account, "synDeposit");
            threadB 
    = new AccessThread(account, "synWithdraw");
            threadA.start();
            threadB.start();
        }
    }



                                                                                                           --    學海無涯
            

    主站蜘蛛池模板: 国产精品免费视频观看拍拍| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 精品国产亚洲男女在线线电影| 另类专区另类专区亚洲| 国产无遮挡吃胸膜奶免费看| 亚洲欧美成人av在线观看| 麻豆国产入口在线观看免费| 一本色道久久综合亚洲精品蜜桃冫| 免费中文熟妇在线影片| 亚洲AV综合永久无码精品天堂| 看全色黄大色大片免费久久| 噜噜噜亚洲色成人网站| 中文字幕亚洲综合久久菠萝蜜 | 免费无码又爽又刺激高潮视频 | 亚洲视频在线精品| 国产真人无码作爱免费视频| 国产亚洲一区二区三区在线| 免费无遮挡无码永久视频| 亚洲国产精品yw在线观看| 在线观看的免费网站| 春暖花开亚洲性无区一区二区 | 中文字幕一区二区三区免费视频| 亚洲精品自产拍在线观看| 久久99国产综合精品免费| 亚洲AV无码一区二区三区牛牛| 韩国日本好看电影免费看| 四虎影视久久久免费| 亚洲四虎永久在线播放| 香蕉高清免费永久在线视频| 久久久久久国产a免费观看不卡| 亚洲成a人片77777老司机| 99久久这里只精品国产免费| 色欲色欲天天天www亚洲伊| 亚洲精品狼友在线播放| av大片在线无码免费| 国产亚洲精彩视频| 亚洲精品高清久久| 波多野结衣免费视频观看| 午夜精品射精入后重之免费观看| 亚洲成a人片在线观看天堂无码| 在线亚洲精品福利网址导航|