那write操作抽取到单独的方法,然后同步那个方法....

解决方案 »

  1.   

    楼上的,刚才跟pigo也讨论了,但为什么要同步呢?
    还有同步write就行了吗?
      

  2.   

    import java.io.*;
    public class Testaaa extends Thread{
    private int i = 0;
    public Testaaa(int i){
    this.i = i;
    }
    public void run(){
    try{
          // System.out.println("1111111");
    FileOutputStream out = new FileOutputStream("C:\\test.txt",true);
    out.write(("start write---------" + i + "\r\n").getBytes());
    out.flush();
    out.close();
     Testd t=Testd.instance();
     t.mA(i);
    Thread.sleep(1000);
    out = new FileOutputStream("C:\\test.txt",true);
    out.write(("end write---------" + i + "\r\n").getBytes());
    out.flush();
    out.close();
        
    }catch(Exception e){
    System.out.println("TestObject--------------" + e.getMessage());
    }
    }
    public static void main(String[] args){
    int i = 0;
    while(i<9){
    new Testaaa(i++).start();
    }
    }
    }class Testd{
    private static Testd t =null;
    private Testd(){
    }
    synchronized public static Testd instance(){
        if(t==null){
            t=new Testd();
        }
        return t;
    }
    public void mA(int i){
    try{
    Thread.sleep(5000);
    FileOutputStream out = new FileOutputStream("C:\\test.txt",true);
    out.write(("Test.mA() write---------" + i + "\r\n").getBytes());
    out.flush();
    out.close();
    }catch(Exception e){
    System.out.println("Test--------------" + e.getMessage());
    }
    }
    }
    这样可以吗?先前线程会一直运行,不会结束,结果那个文件会被写的无穷大
      

  3.   

    看来write方法就已经开始和文件交流了。大家能否具体说说文件操作的细过程,难道在写入数据的时候会删除以前的文件吗?要不怎么会出现 文件存在 的错误?
    import java.io.*;
    public class TestObject extends Thread{
    private int i = 0;
    public TestObject(int i){
    this.i = i;
    }
    public void run(){
    try{
    FileOutputStream out = new FileOutputStream("C:/test.txt",true);
    Test.t.w(out, "start write---------" + i + "\r\n");
    out.flush();
    out.close();
    Test.t.mA(i);
    Thread.sleep(1000);
    out = new FileOutputStream("C:/test.txt",true);
    Test.t.w(out, "end write---------" + i + "\r\n");
    out.flush();
    out.close();
    }catch(Exception e){
    System.out.println("TestObject--------------" + e.getMessage());
    }
    }
    public static void main(String[] args){
    int i = 0;
    while(true){
    new TestObject(i++).start();
    }
    }
    }class Test{
    public static Test t = new Test();
    public synchronized void w(FileOutputStream out, String str){
    try{
    out.write(str.getBytes());
    }catch(Exception e){
    System.out.println("Test.w--------------" + e.getMessage());
    }
    }
    public void mA(int i){
    try{
    Thread.sleep(5000);
    FileOutputStream out = new FileOutputStream("C:/test.txt",true);
    w(out, "Test.mA() write---------" + i + "\r\n");
    out.flush();
    out.close();
    }catch(Exception e){
    System.out.println("Test.mA--------------" + e.getMessage());
    }
    }
    }
      

  4.   

    //试试这个
    import java.io.FileOutputStream;public class TestObject extends Thread
    {
        private int i = 0;
        public TestObject(int i)
        {
            this.i = i;
        }    public void run()
        {
            try
            {
                FileOutputStream out = new FileOutputStream("C:/test.txt",true);
                Test.t.w(out,"start write---------" + i + "\r\n");
                out.flush();
                out.close();
                Test.t.mA(i);
                Thread.sleep(1000);
                out = new FileOutputStream("C:/test.txt",true);
                Test.t.w(out,"end write---------" + i + "\r\n");        }
            catch(Exception e)
            {
                System.out.println("TestObject--------------" + e.getMessage());
            }
        }    public static void main(String[] args)
        {
            int i = 0;
            while(true)
            //for(int j = 0;j < 100;j++)
            {
                new TestObject(i++).start();
            }
        }
    }class Test
    {
        public static Test t = new Test();
        public synchronized void w(FileOutputStream out,String str)
        {
            try
            {
                out.write(str.getBytes());
                out.flush();
                out.close();
            }
            catch(Exception e)
            {
                System.out.println("Test.w--------------" + e.getMessage());
            }
        }    public void mA(int i)
        {
            try
            {
                Thread.sleep(5000);
                FileOutputStream out = new FileOutputStream("C:/test.txt",true);
                w(out,"Test.mA() write---------" + i + "\r\n");
                out.flush();
                out.close();
            }
            catch(Exception e)
            {
                System.out.println("Test.mA--------------" + e.getMessage());
            }
        }
    }