zoukankan      html  css  js  c++  java
  • 实现多线程的同时复制(三个线程同时复制)

     1 package com.threadcopyfile;
     2 
     3 /**
     4  * @author lisj
     5  * 实现多线程的同时复制
     6  * 调用多个线程同时复制各自的模块
     7  */
     8 public class threadCopy {
     9     
    10     
    11     public static void main (String args[]){
    12         
    13         ThreadCopyFile a=new ThreadCopyFile(1);    //实例化多个线程
    14         ThreadCopyFile b=new ThreadCopyFile(2);
    15         ThreadCopyFile c=new ThreadCopyFile(3);
    16         Thread down1 = new Thread(a);
    17         down1.start();            //启动线程1,2,3
    18         Thread down2 = new Thread(b);
    19         down2.start();            //
    20         Thread down3 = new Thread(c);
    21         down3.start();
    22             
    23     }
    24     
    25 }
     1 package com.threadcopyfile;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 import java.io.RandomAccessFile;
     6 
     7 /**
     8  * @author lisj
     9  * 实现复制功能
    10  * 当线程调用的时候开始复制
    11  */
    12 public class ThreadCopyFile extends Thread{
    13 
    14     int i;    //定义全局变量i,识别复制的模块数
    15     
    16     ThreadCopyFile(int i)
    17     {
    18         this.i=i;
    19     }
    20     
    21     
    22 /**
    23  * 文件复制函数
    24  * 利用RandomAccessFile类实现文件的读和写
    25  * 实现复制文件的功能
    26  */
    27  public void run() {
    28       
    29           System.out.println("线程"+i+"运行中。。。");
    30     
    31           File ofile=new File("e:/java乱码处理.txt");    
    32           File nfile=new File("e:/456.txt");    //定义目的路径以及文件名
    33             
    34             try {
    35                 
    36                 RandomAccessFile in=new RandomAccessFile(ofile,"rw"); 
    37                             
    38                 long length=in.length()/3;
    39                 
    40                 RandomAccessFile out=new RandomAccessFile(nfile,"rw");
    41                 int count=0;
    42                 int len=0;
    43                 byte[] b= new byte[2048];
    44                 in.seek(length*(i-1));    //设置读文件偏移位置
    45                 out.seek(length*(i-1)); //设置写文件偏移位置
    46                 while(((len=in.read(b))!=-1)&&(count<=(int)length)){    //读取文件内容设置写文件停止条件
    47                     
    48                         out.write(b, 0, len);    
    49                         count=count+len;
    50                         
    51                     }//写出文件内容
    52                         
    53             } catch (IOException e) {
    54                 
    55                 e.printStackTrace();
    56             }
    57             
    58             System.out.println("线程"+i+"复制完成!");
    59         }
    60   
    61     }
  • 相关阅读:
    Spark ML参考博客
    Mysql数据库相关
    git clone server certificate verification failed
    GSM网络扫频结果
    USRP E310启用SSH的X11 Forwarding功能
    Win 7 SVN查看日志时出现There has been a problem contacting the server
    OpenWrt修改MAC地址
    NETGEAR WNDR4300路由器tftp方式 刷机
    ubuntu系统中修改hostname
    USRP中sampling rate和master clock rate的区别
  • 原文地址:https://www.cnblogs.com/lifescolor/p/3874104.html
Copyright © 2011-2022 走看看