文件读写线程-写线程依赖读线程(减少文件读写时间),多线程

前端之家收集整理的这篇文章主要介绍了文件读写线程-写线程依赖读线程(减少文件读写时间),多线程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件读写线程:
使用synchronized 配合wait/notify,稳定性好,线程数越多越快

package cn.thread.fen.open;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

public class FileReadThread extends Thread{
private FileInputStream is;
private int bufSize=1024;
private volatile byte[] buf=new byte[1024];
private volatile int len = 0;

public void run() {
try {
synchronized(this) {
while (this.getLen()!=-1) { //读完整个文件,len=-1
if (this.getLen() != 0) {
wait(10);
}
else{
int length = this.getIs().read(this.getBuf(),this.bufSize);
this.setLen(length);
notify();

//System.out.println("----------"+length);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
try {
this.getIs().close();
} catch (IOException e) {
e.printStackTrace();
}
String readTime=new Date().toString();
System.out.println("========== readOver Time : "+readTime +"==========");
}
}

public FileInputStream getIs() {
return is;
}

public void setIs(FileInputStream is) {
this.is = is;
}

public byte[] getBuf() {
return buf;
}

public void setBuf(byte[] buf) {
this.buf = buf;
}

public int getLen() {
return len;
}

public void setLen(int len) {
this.len = len;
}

}

package cn.thread.fen.open;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class FileWriteThread extends Thread {
//private volatile byte[] buf=new byte[1024];//文件读写公用的缓存数据
//private volatile int len = 0;//读取的字符长度,公用
private FileOutputStream os;
private FileReadThread r;

public FileWriteThread(FileReadThread r){
this.r=r;
}

public void run() {
synchronized(this){
while(true){
if(r.getLen()==0){
try {
wait(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else{
//System.out.println("*********"+r.getLen());
if(r.getLen()==-1){ //读线程结束,写线程也结束
try{
this.getOs().close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
this.getOs().write(r.getBuf(),r.getLen());
//os.flush();
} catch (IOException e) {
e.printStackTrace();
}

r.setLen(0);
notify();
}
}
}
String writeTime=new Date().toString();
System.out.println("========== writeOver Time : "+writeTime +"==========");
}

public FileOutputStream getOs() {
return os;
}

public void setOs(FileOutputStream os) {
this.os = os;
}


}

package cn.thread.fen.open;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

public class TestThread {

/**
*
*/
public static void main(String[] args) throws Exception {

//String startTime=new Date().toString();
//System.out.println("========== startTime : "+startTime +"==========");
//
//File file = new File("E:\\Log\\linkType.txt");
//int i=(int)(Math.random()*100);
//File of = new File("E:\\Log\\"+i+".txt");
//
//FileReadThread r = new FileReadThread();
//r.setIs(new FileInputStream(file));
//FileWriteThread w = new FileWriteThread(r);
//w.setOs(new FileOutputStream(of));
//
//r.start(); //起动读线程
//w.start();//起动写线程


test();

}

private static void test(){
String startTime=new Date().toString();
System.out.println("========== startTime : "+startTime +"==========");

File file = new File("E:\\Log\\bc2.txt");
int i=(int)(Math.random()*100);
File of = new File("E:\\Log\\"+i+".txt");

FileReadThread r = new FileReadThread();
try {
r.setIs(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileWriteThread w = new FileWriteThread(r);
try {
w.setOs(new FileOutputStream(of));
} catch (FileNotFoundException e) {
e.printStackTrace();
}

r.start(); //起动读线程w.start();//起动写线程 }}

原文链接:https://www.f2er.com/javaschema/286610.html

猜你在找的设计模式相关文章