打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java 多线程文件拷贝
多线程文件拷贝
要求:线程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();
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java压缩解压代码
java将office文档pdf文档转换成swf文件在线预览
java数据库操作类
Java------异常处理机制
死锁问题
java中多线程产生死锁的原因以及解决意见
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服