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

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

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

    weidagang2046的專欄

    物格而后知致
    隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
    數據加載中……

    多線程文件拷貝

    要求:線程1從文件f:\1.txt讀取文件,線程2通過線程1將內容保存至f:\2.txt。

    import?java.io.*;


    class?Buffer
    {
    ????
    private?char[]?contents;

    ????
    private?int?size;

    ????
    private?int?state;

    ????
    public?final?static?int?FULL?=?0;

    ????
    public?final?static?int?EMPTY?=?1;

    ????
    public?final?static?int?UNSTABLE?=?2;

    ????
    public?final?static?int?END?=?3;
    ????
    ????
    public?Buffer()
    ????{
    ????????state?
    =?EMPTY;
    ????}

    ????
    public?char[]?getContents()
    ????{
    ????????
    return?contents;
    ????}

    ????
    public?void?setContents(char[]?contents,?int?size)
    ????{
    ????????
    this.contents?=?contents;
    ????????
    this.size?=?size;
    ????}

    ????
    public?int?getSize()
    ????{
    ????????
    return?size;
    ????}

    ????
    synchronized?public?int?getState()
    ????{
    ????????
    return?state;
    ????}
    ????
    ????
    synchronized?public?void?setState(int?state)
    ????{
    ????????
    this.state?=?state;
    ????}
    }

    class?Thread1?extends?Thread
    {
    ????
    private?String?inputFile;
    ????
    private?Buffer?buffer;
    ????
    ????
    public?Thread1(String?inputFile)
    ????{
    ????????
    this.inputFile?=?inputFile;
    ????????
    this.buffer?=?new?Buffer();
    ????}

    ????
    public?void?run()
    ????{
    ????????
    try
    ????????{
    ????????????BufferedReader?in?
    =?new?BufferedReader(new?FileReader(new?File(
    ????????????????????inputFile)));
    ????????????
    int?size?=?0;
    ????????????
    while(true)
    ????????????{
    ????????????????
    char[]?contents?=?new?char[102];
    ????????????????size?
    =?in.read(contents,?0,?100);
    ????????????????
    if(size?<=?0)
    ????????????????????
    break;
    ????????????????
    synchronized(buffer)
    ????????????????{
    ????????????????????
    if(Buffer.EMPTY?!=?buffer.getState())
    ????????????????????{
    ????????????????????????buffer.wait();
    ????????????????????}
    ????????????????????System.out.println(
    "Thread1:?"?+?size);
    ????????????????????buffer.setContents(contents,?size);
    ????????????????????buffer.setState(Buffer.FULL);
    ????????????????????buffer.notify();
    ????????????????}
    ????????????}
    ????????????buffer.setState(Buffer.END);
    ????????????
    synchronized(buffer)
    ????????????{????
    ????????????????buffer.notify();
    ????????????}
    ????????????in.close();
    ????????}?
    catch?(FileNotFoundException?e)
    ????????{
    ????????????System.err.println(e.getMessage());
    ????????????e.printStackTrace();
    ????????}?
    catch?(IOException?e)
    ????????{
    ????????????System.err.println(e.getMessage());
    ????????????e.printStackTrace();
    ????????}?
    catch?(InterruptedException?e)
    ????????{
    ????????????System.err.println(e.getMessage());
    ????????????e.printStackTrace();
    ????????}
    ????}

    ????
    public?Buffer?getBuffer()
    ????{
    ????????
    return?buffer;
    ????}
    }

    class?Thread2?extends?Thread
    {
    ????
    private?String?outputFile;

    ????
    private?Buffer?buffer;
    ????
    ????
    public?Thread2(String?outputFile,?Buffer?buffer)
    ????{
    ????????
    this.outputFile?=?outputFile;
    ????????
    this.buffer?=?buffer;
    ????}

    ????
    public?void?run()
    ????{
    ????????
    try
    ????????{
    ????????????BufferedWriter?out?
    =?new?BufferedWriter(new?FileWriter(
    ????????????????????
    new?File(outputFile)));
    ????????????
    while(true)
    ????????????{
    ????????????????
    synchronized(buffer)
    ????????????????{
    ????????????????????
    int?state;
    ????????????????????
    while(Buffer.FULL?!=?(state?=?buffer.getState()))
    ????????????????????{
    ????????????????????????
    if(Buffer.END?==?state)
    ????????????????????????????
    break;
    ????????????????????????buffer.wait();
    ????????????????????}
    ????????????????????
    if(Buffer.END?==?state)
    ????????????????????????
    break;
    ????????????????????System.out.println(
    "Thread2:?"?+?buffer.getSize());
    ????????????????????out.append(
    new?String(buffer.getContents(),?0,?buffer.getSize()));
    ????????????????????buffer.setState(Buffer.EMPTY);
    ????????????????????buffer.notify();
    ????????????????}
    ????????????}
    ????????????out.close();
    ????????}?
    catch?(IOException?e)
    ????????{
    ????????????System.err.println(e.getMessage());
    ????????????e.printStackTrace();
    ????????}?
    catch?(InterruptedException?e)
    ????????{
    ????????????System.err.println(e.getMessage());
    ????????????e.printStackTrace();
    ????????}
    ????}
    }

    public?class?FileCopy
    {

    ????
    public?static?void?main(String[]?argv)
    ????{
    ????????Thread1?thread1?
    =?new?Thread1("f:\\1.txt");
    ????????Thread2?thread2?
    =?new?Thread2("f:\\2.txt",?thread1.getBuffer());
    ????????
    ????????thread1.start();
    ????????thread2.start();
    ????}
    }

    posted on 2006-06-12 22:34 weidagang2046 閱讀(871) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 免费人成视网站在线观看不卡| 免费人妻av无码专区| 成人午夜亚洲精品无码网站| 中文字幕专区在线亚洲| 在线电影你懂的亚洲| 亚洲乱色熟女一区二区三区蜜臀| 无码免费又爽又高潮喷水的视频| 9久久免费国产精品特黄| 精品久久久久久久久免费影院| 国产乱人免费视频| 亚洲精品国产免费| eeuss影院www天堂免费| 免费大片黄在线观看yw| 天天爽亚洲中文字幕| 99在线视频免费观看| 免费毛片在线视频| 久久久久无码精品亚洲日韩| 国产亚洲一卡2卡3卡4卡新区| 久久精品成人免费看| 成人永久免费福利视频网站| 亚洲福利视频网站| 国产精品视频免费一区二区| 亚洲国产精品无码久久久不卡| 亚洲aⅴ无码专区在线观看春色 | 成人免费看吃奶视频网站| 亚洲乱码日产一区三区| 国产精品亚洲片在线花蝴蝶| 国产精品免费精品自在线观看| 久久亚洲国产成人精品无码区| a级毛片在线免费| 亚洲春黄在线观看| 99久久免费精品高清特色大片| 亚洲精品动漫人成3d在线| 亚洲高清国产拍精品熟女| 成人免费午夜视频| 中文字幕在线成人免费看| 亚洲一区二区三区香蕉| 巨胸喷奶水www永久免费| 亚洲日韩中文字幕| 亚洲天堂免费在线视频| 黄色一级视频免费|