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

          主站蜘蛛池模板: 瓦房店市| 新龙县| 惠州市| 宿迁市| 阿巴嘎旗| 太原市| 临潭县| 化州市| 米泉市| 武宁县| 织金县| 淮安市| 肇东市| 萍乡市| 沙坪坝区| 石林| 深泽县| 乐亭县| 三江| 鄂温| 娱乐| 玉溪市| 梁山县| 汝阳县| 偃师市| 南开区| 蒙自县| 广昌县| 正蓝旗| 四会市| 乌拉特后旗| 夏邑县| 屏东市| 祁门县| 岑溪市| 昌黎县| 泰兴市| 都昌县| 揭阳市| 蓬溪县| 普格县|