亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java多線程中的Balking模式詳解

 更新時(shí)間:2022年01月25日 10:39:54   作者:腦機(jī)接口社區(qū)  
大家好,本篇文章主要講的是Java多線程中的Balking模式詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下

1.場(chǎng)景

自動(dòng)保存功能:

為防止電腦死機(jī),而定期將數(shù)據(jù)內(nèi)容保存到文件中的功能。

2.詳細(xì)說(shuō)明

當(dāng)數(shù)據(jù)內(nèi)容被修改時(shí),內(nèi)容才會(huì)被保存。即當(dāng)寫入的內(nèi)容與上次寫入的內(nèi)容一致時(shí),其實(shí)就沒有必要執(zhí)行寫入操作。也就是說(shuō),以”數(shù)據(jù)內(nèi)容是否一致”作為守護(hù)條件。若數(shù)據(jù)內(nèi)容相同,則不執(zhí)行寫入操作,直接返回。

3.Balking模式的本質(zhì):停止并返回

如果現(xiàn)在不合適執(zhí)行該操作,或者沒有必要執(zhí)行該操作,就停止處理,直接返回—-Balking模式。

要實(shí)現(xiàn)上述描述的場(chǎng)景,我們需要設(shè)計(jì)四個(gè)類:數(shù)據(jù)類Data,修改內(nèi)容的線程類WriterThread,保存數(shù)據(jù)的線程類,啟動(dòng)線程的類。類圖如下:

Java多線程模式-Balking模式_多線程

當(dāng)數(shù)據(jù)類Data里的內(nèi)容被修改時(shí),守護(hù)條件”數(shù)據(jù)內(nèi)容是否一致”變?yōu)閒alse,則進(jìn)行保存工作:保存工作可由定時(shí)保存功能完成也可由修改內(nèi)容的線程類完成。如果守護(hù)條件為true,則直接返回,不執(zhí)行保存操作。

源代碼如下:

數(shù)據(jù)類

public class Data{
    private final String filename;//保存文件的名稱
    private String content;//數(shù)據(jù)的內(nèi)容
    private boolean changed;//內(nèi)容是否已保存的標(biāo)志,若修改后為保存,則為true

    public Data(String filename,String content){
        this.filename=filename;
        this.content=content;
        this.changed=true;
    }
    //修改數(shù)據(jù)內(nèi)容
    public synchronized void change(String newContent){
        content=newContent;
        changed=true;
    }

    //若數(shù)據(jù)內(nèi)容修改過(guò),則保存到文件中
    public synchronized void save() throws IOException{
        if(!changed){
            return;
        }
        doSave();
        changed=false;
    }
    //將數(shù)據(jù)保存到文件中
    private void doSave() throws IOException {
        // TODO Auto-generated method stub
        StdOut.println(Thread.currentThread().getName()+" calls doSave,content= "+content);
        Writer writer=new FileWriter(filename);
        writer.write(content);
        writer.close();
    }
}

修改內(nèi)容的線程類

public class WriterThread extends Thread {
    private final Data data;
    private final Random random=new Random();
    public WriterThread(String name,Data data){
        super(name);
        this.data=data;
    }
    public void run(){
        try {
            for(int i=0;true;i++){
                data.change("NO."+i);//修改數(shù)據(jù)
                Thread.sleep(random.nextInt(1000));//執(zhí)行其他操作
                data.save();
            }
        } catch (InterruptedException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

定時(shí)保存數(shù)據(jù)的線程類

public class SaverThread extends Thread {
    private final Data data;
    public SaverThread(String name,Data data){
        super(name);
        this.data=data;
    }

    public void run(){
        try {
            while(true){
                data.save();//保存數(shù)據(jù)
                Thread.sleep(1000);//休眠約1秒
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

啟動(dòng)線程的類

public class Main {

    public static void main(String[] args) {
        Data data=new Data("data1.txt", "empty");
        new WriterThread("ChangerThead",data).start();
        new SaverThread("SaverThread", data).start();
    }
}

總結(jié)

到此這篇關(guān)于Java多線程中的Balking模式詳解的文章就介紹到這了,更多相關(guān)Java多線程Balking模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論