RandomAccessFile 多线程的文件复制

 
package namespace;
import java.io.*;

class Download implements Runnable
{
	private String source;
	private String dest;
	private int buflen=1024*1024;
	private int threadnum=5;//定义线程个数
	private Thread thread[];//定义线程数组
	public Download(String source,String dest)
	{
		this.source=source;	
		this.dest=dest;
	}
	public Download(String source,String dest,int threadnum)
	{
		this.source=source;
		this.dest=dest;
		this.threadnum=threadnum;
	}
	public void getThread()
	{
		thread = new Thread[threadnum];
		for(int i=0;i<thread.length;i++)
			{
				thread[i]=new Thread(this);
				thread[i].setName(i+"");//设置线程的名字,用于区分其他线程
			}
	}
	public void run()
	{
		int id=Integer.valueOf(Thread.currentThread().getName());
		
		byte buffer[] = new byte [buflen];
		try { 
			RandomAccessFile reader=new RandomAccessFile(source,"r");//为每个线程定义一个读取的对象
			RandomAccessFile writer= new RandomAccessFile(dest,"rw");
			 long alone=reader.length()%threadnum==0?reader.length()/threadnum:((int)(Math.ceil((double)reader.length()/threadnum)));//计算每个线程应该读取的长度
			 long begin=id*alone;	//第个线程读取文件的开始位置
			 reader.seek(begin);//定位到开始位置
			 writer.seek(begin);
			 long end=(begin+alone)>reader.length()?reader.length():(begin+alone);//计算每个线程的结束位置
			 while(begin<end)
			 {
				 int len=0;
				 if(begin+buflen<end)//如果可以装满一个缓冲区
				 {
					 len=reader.read(buffer);
					
				 }else
				 {
					 len=reader.read(buffer,0,(int)(end-begin));
				 }
				 writer.write(buffer,0,len); 
				 begin+=len;
				 System.out.println("Thread-"+id+"get "+len);
			 }
		}catch(IOException ex)
		{
			System.out.println(ex);
		}
	}
	public int getThreadNumber()
	{
		return this.thread.length;
	}
	public void start()
	{
		for(int i=0;i<thread.length;i++)
			thread[i].start();
	}	
}
public class RandomAccessDeom {

	public static void main(String args[]) throws Exception 
	{
		/*long begin=System.currentTimeMillis();
		Download load = new Download("F:\\movie.rmvb","D:\\movie.rmvb",5);
		load.getThread();
		load.start();
		while(true)
		{
			if(Thread.activeCount()==1)
				{
				long end=System.currentTimeMillis();
				System.out.println("Download successfully..."+(end-begin)+"ms");
				break;
				}
		}*/
		 download();
	}
	//写一个单纯种的下载方法与多纯种的进行一下比较
	public static void download() throws IOException
	{
		
		long begin=System.currentTimeMillis();
		BufferedInputStream filein = new BufferedInputStream(new FileInputStream("F:\\movie.rmvb"));
		BufferedOutputStream fileout = new BufferedOutputStream(new FileOutputStream("D:\\movie.rmvb"));
		byte buffer [] = new byte [1024*1024];
		int length=0;
		while((length=filein.read(buffer))!=-1)
		{
			fileout.write(buffer,0,length);
			System.out.println("Download "+length);
		}
		fileout.close();
		filein.close();
		long end=System.currentTimeMillis();
		System.out.println("Download successfully..."+(end-begin)+"ms");
	}

}

/*
 * 
 * 		多线程文件的复制
 * 
 * 		原理:利用RandomAccessFile 来为每个线程开一个读取和写入的对象
 * 		为每个线程计算出要读取文件的开始位置和写入位置,然后再用相应的写入方法写入到文件中
 * 
 * 		不上面的单线程复制和多线程比较,貌似单线程的比我线程要快的多.........
 *													2011/11/3  20:25:54		 
 * */

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值