首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

java 多线程读写资料,wait,notify

2012-11-03 
java 多线程读写文件,wait,notifyimport java.io.FileInputStreamimport java.io.FileNotFoundException

java 多线程读写文件,wait,notify

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtil {

??? FileInputStream fis;
??? byte[] buf=new byte[1024];
??? FileOutputStream fos;
??? int len=0;
??? public static void main(String[] args) {

??? ??? FileUtil fu=new FileUtil();
//??? ??? String srcFile="F:/test.properties";
//??? ??? String desFile="F:/test.txt";
??? //??? String srcFile="F:/B-初步.ppt"; //java.io.FileNotFoundException,为啥 不支持ppt呢?
//??? ??? String srcFile="F:/部署文档.doc";? //中文 可以访问,word 版本要一致,不然文件打不开
//??? ??? String srcFile="F:/t.doc";
??? ??? String srcFile="F:/t.ppt";? //支持ppt,但是打不开文件
??? ??? String desFile="F:/test.ppt";
??? ??? fu.init(srcFile, desFile);
??? ???
??? ??? fu.new ReadFileThread().start();
??? ??? fu.new WriteFileThread().start();
??? }
???
??? /**
??? ?* 初始化,输入流,输出流
??? ?* @param srcFile 读取的文件名
??? ?* @param desFile 写入的文件名
??? ?*/
??? void init(String srcFile,String desFile){
??? ??? try {
??? ??? ??? fis=new FileInputStream(srcFile);
??? ??? ??? fos=new FileOutputStream(desFile);
??? ??? } catch (FileNotFoundException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
???
??? }
???
??? /**
??? ?* 读取一个文件到buf数组中,先读一次,再wait()
??? ?* @author Administrator
??? ?*
??? ?*/
??? class ReadFileThread extends Thread{
??? ??? public void run(){
??? ??? ??? try{
?
??? ??? ??? ??? synchronized(this){

??? ??? ??? ??? ??? if(len!=0){
??? ??? ??? ??? ??? ??? try {
??? ??? ??? ??? ??? ??? ??? wait();
??? ??? ??? ??? ??? ??? } catch (InterruptedException e) {
??? ??? ??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? len=fis.read(buf);
??? ??? ??? ??? ??? notify();????????????????????????? //唤醒写入线程
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ??? catch(IOException e){
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? ??? finally{
??? ??? ??? ???
??? ??? ??? }
??? ??? }
??? }

??? /**
??? ?* 写入另一个文件,如果读取的长度len!=0,就把buf的内容写入到另一个文件,并且把len=0;换醒读取线程
??? ?* @author Administrator
??? ?*
??? ?*/
??? class WriteFileThread extends Thread{
??? ??? public void run() {
??? ??? ??? synchronized(this){
??? ??? ??? ??? if(len==0){
??? ??? ??? ??? ??? try {
??? ??? ??? ??? ??? ??? wait();
??? ??? ??? ??? ??? } catch (InterruptedException e) {
??? ??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? ??? try {
??? ??? ??? ??? ??? fos.write(buf,0,len);
??? ??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? }
??? ??? ??? ??? len=0;
??? ??? ??? ??? notify();
??? ??? ??? }
??? ??? }
??? }
}

?

注意:java.lang.IllegalMonitorStateException: current thread not owner?

对于上述方法,只有在当前线程中才能使用,否则报运行时错误java.lang.IllegalMonitorStateException: current thread not owner.

从实现角度来分析:
在线程调用wait()方法时,需要把它放到一个同步段里,即应该在调用前使用
1synchroed(this){
2? thread.wait();
3 ?
4}
5
否则将会出现"java.lang.IllegalMonitorStateException: current thread not owner"的异常。

热点排行