weidagang2046的专栏

物格而后知致
随笔 - 8, 文章 - 409, 评论 - 101, 引用 - 0
数据加载中……

多线程文件拷贝

要求:线程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, 0100);
                
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();
    }
}

posted on 2006-06-12 22:34 weidagang2046 阅读(869) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航: