繁体   English   中英

同步写入和读取文件,但不加入文件的对象变量

[英]Synchronized writing and reading in file with not join object variable of file

我正在尝试在Java中实现线程并发。 它包括尝试最后一次写入文件。 有两个线程: A正在创建文件并检查右行是否在文件中; B正在搜索文件并尝试以“ good”行重写文件。 “优胜者”线程必须在文件中包含字符串。 为此,线程检查文件是否有一行,文件只有一行。 线程只有文件路径。

public class A implements Runnable {
private File file;
private Thread t;

public A(String patch,String fileName)
{
    t = new Thread(this);
    CreateFile(patch, fileName);
    //t.setDaemon(true);
    t.start();
}

@Override
public void run() {
    BufferedReader reader;
    while (!Thread.currentThread().isInterrupted()) {
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            if (reader.readLine().charAt(0) == 'B') {
                System.out.println("A try took file: " + file.getName());
                write();
            } else {
                System.out.println("A took file: " + file.getName());
            }
        } catch (FileNotFoundException e)
        {
            System.out.println("File read A" + e.toString());
        }
        catch (IOException e)
        {
            System.out.println("File read A"+e.toString());
        }
    }
}


private void write() {
    try {
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.println("A took file: " + file.getName());
        System.out.println("A took file: " + file.getName());
        printWriter.close();
    } catch (Exception e) {
        System.out.println("File write A");
    }
}

public File CreateFile(String patch,String fileName) {
    File file = new File(patch,fileName+".txt");
    try {
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.println("A took file: " + file.getName());
        System.out.println("A took file: " + file.getName());
        printWriter.close();
    } catch (Exception e) {
        System.out.println("File create A");
    }
    return file;
}
}

public class B implements Runnable {
private File file;
private Thread t;

public B(String patch,String fileName)
{
    t = new Thread(this);
    //t.setDaemon(true);
    FindFile(patch, fileName);
    t.start();
}

@Override
public void run() {
    BufferedReader reader;
    while (!Thread.currentThread().isInterrupted()) {
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            if (reader.readLine().charAt(0) == 'A') {
                System.out.println("B try took file: " + file.getName());
                write();
            } else {
                System.out.println("B took file: " + file.getName());
            }
        } catch (FileNotFoundException e)
        {
            System.out.println("File read B" + e.toString());
        }
        catch (IOException e)
        {
            System.out.println("File read B"+e.toString());
        }
    }
}


private void write() {
    try {
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.println("B took file: " + file.getName());
        System.out.println("B took file: " + file.getName());
        printWriter.close();
    } catch (Exception e) {
        System.out.println("File write B");
    }
}

public File FindFile(String patch,String fileName) {
    File file= null;
    File folder = new File(patch);
    File[] listOfFiles = folder.listFiles();
    BufferedReader reader;
    for (int i = 0; i < listOfFiles.length; i++) {
        file = listOfFiles[i];
        if (file.getName().equals(fileName + ".txt")) {
            break;
        }
    }
    return file;
}
}

我想以某种方式同步对线程中文件的访问。 在我的代码中,当我使用readLine() ,我有java.lang.NullPointerException ,所以我认为这是因为线程没有对文件的同步访问(在每个完成的操作文件必须有一行之后)。 我不能使用同步方法或块,因为线程没有文件的联合变量。 有什么方法可以使文件中的读写同步?

您可以使用ReentrantReadWriteLock进行同步。 解析相同的锁以同步A和B。在这里,我修改了B。您也可以对A进行相同的操作。

public static class B implements Runnable {
        private File file;
        private Thread t;
        private ReentrantReadWriteLock lock;

        public B(String patch,String fileName, ReentrantReadWriteLock lock)
        {
            t = new Thread(this);
            this.lock = lock;
            //t.setDaemon(true);
            FindFile(patch, fileName);
            t.start();
        }

        @Override
        public void run() {
            BufferedReader reader;
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    lock.readLock().lock();
                    reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                    if (reader.readLine().charAt(0) == 'A') {
                        lock.readLock().unlock();
                        lock.writeLock().lock();
                        System.out.println("B try took file: " + file.getName());
                        write();
                        lock.writeLock().unlock();
                    } else {
                        System.out.println("B took file: " + file.getName());
                    }
                } catch (FileNotFoundException e)
                {
                    System.out.println("File read B" + e.toString());
                }
                catch (IOException e)
                {
                    System.out.println("File read B"+e.toString());
                }
            }
        }


        private void write() {
            try {
                PrintWriter printWriter = new PrintWriter(file);
                printWriter.println("B took file: " + file.getName());
                System.out.println("B took file: " + file.getName());
                printWriter.close();
            } catch (Exception e) {
                System.out.println("File write B");
            }
        }

        public File FindFile(String patch,String fileName) {
            File file= null;
            File folder = new File(patch);
            File[] listOfFiles = folder.listFiles();
            BufferedReader reader;
            for (int i = 0; i < listOfFiles.length; i++) {
                file = listOfFiles[i];
                if (file.getName().equals(fileName + ".txt")) {
                    break;
                }
            }
            return file;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM