<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 閱讀(875) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 一级毛片a女人刺激视频免费| 亚洲色欲色欱wwW在线| 中国毛片免费观看| 亚洲精品第一国产综合精品99 | 婷婷亚洲综合五月天小说| 好吊色永久免费视频大全| 黑人大战亚洲人精品一区| 国产精品hd免费观看| 狠狠色伊人亚洲综合成人| 鲁丝片一区二区三区免费 | 在线电影你懂的亚洲| 18女人水真多免费高清毛片| 亚洲美女视频一区| 毛片a级毛片免费观看免下载 | 一级一级一级毛片免费毛片| 日韩精品亚洲aⅴ在线影院| 久久爰www免费人成| 亚洲欧洲春色校园另类小说| 成年人网站在线免费观看| 亚洲av色香蕉一区二区三区蜜桃| 免费一级毛片一级毛片aa| 国产日韩在线视频免费播放| 亚洲自偷自拍另类12p| 久久精品网站免费观看| 国产在亚洲线视频观看| 亚洲中文字幕无码一区| 最近中文字幕mv免费高清在线| 中文日韩亚洲欧美制服| 亚洲狠狠爱综合影院婷婷| 野花香高清视频在线观看免费| 91亚洲国产成人精品下载| 免费看大美女大黄大色| 中文字幕视频免费在线观看| 亚洲国产高清美女在线观看| 国产成人免费a在线视频色戒| 日本免费中文字幕| 亚洲午夜成人精品无码色欲| 亚洲一区爱区精品无码| 在线观看免费大黄网站| 免费无码又爽又刺激高潮软件| 亚洲免费福利在线视频|