欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java可重入鎖與不可重入鎖怎么寫

本文小編為大家詳細(xì)介紹“Java可重入鎖與不可重入鎖怎么寫”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java可重入鎖與不可重入鎖怎么寫”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)成都多線機(jī)房的網(wǎng)絡(luò)公司,有著豐富的建站經(jīng)驗(yàn)和案例。

不可重入鎖

//不可重入鎖public class LockTest {Lock lock=new Lock();public void a() throws InterruptedException {lock.lock();
        b();lock.unlock();
    }//不可重入    public void b() throws InterruptedException {lock.lock();       //........        lock.unlock();
    }public static void main(String[] args) throws InterruptedException {LockTest lockTest=new LockTest();lockTest.a();lockTest.b();

    }
    
}class Lock{//是否占用    private boolean flag=false;//使用鎖    public synchronized void lock() throws InterruptedException {while(flag){
            wait();

        }flag=true;

    }public synchronized void unlock(){flag=false;
        notify();
    }
}

可重入鎖

//可重入鎖public class LockTest {ReLock lock=new ReLock();public void a() throws InterruptedException {lock.lock();System.out.println(lock.getCount());
        b();lock.unlock();System.out.println(lock.getCount());
    }//不可重入    public void b() throws InterruptedException {lock.lock();System.out.println(lock.getCount());       //........        lock.unlock();System.out.println(lock.getCount());
    }public static void main(String[] args) throws InterruptedException {LockTest lockTest=new LockTest();lockTest.a();Thread.sleep(1000);System.out.println(lockTest.lock.getCount());

    }

}//可重入鎖class ReLock{//是否占用    private boolean flag=false;private Thread LockedBY=null;//存儲(chǔ)線程    private int count=0;//使用鎖    public synchronized void lock() throws InterruptedException {Thread t=Thread.currentThread();while(flag&&LockedBY!=t){
            wait();

        }flag=true;LockedBY=t;count++;


    }public synchronized void unlock(){if(Thread.currentThread()==LockedBY){count--;if(count==0){flag=false;
                notify();LockedBY=null;
            }

        }

    }public int getCount() {return count;
    }
}

讀到這里,這篇“Java可重入鎖與不可重入鎖怎么寫”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章標(biāo)題:Java可重入鎖與不可重入鎖怎么寫
新聞來源:http://chinadenli.net/article36/gjcipg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)品牌網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站面包屑導(dǎo)航、搜索引擎優(yōu)化品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)