有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从多个线程写入文本文件?

我有20个线程在名为results的文件上使用println()函数编写。txt。如何同步它们

我注意到每次程序运行时,结果中的文本行数都不同。txt

多谢各位


共 (3) 个答案

  1. # 1 楼答案

    重复问题。。。重复回答As I said here:

    如果您可以将文件保存为FileOutputStream,则可以如下方式锁定它:

    FileOutputStream file = ...
    ....
    // Thread safe version.
    void write(byte[] bytes) {
      try {
        boolean written = false;
        do {
          try {
            // Lock it!
            FileLock lock = file.getChannel().lock();
            try {
              // Write the bytes.
              file.write(bytes);
              written = true;
            } finally {
              // Release the lock.
              lock.release();
            }
          } catch ( OverlappingFileLockException ofle ) {
            try {
              // Wait a bit
              Thread.sleep(0);
            } catch (InterruptedException ex) {
              throw new InterruptedIOException ("Interrupted waiting for a file lock.");
            }
          }
        } while (!written);
      } catch (IOException ex) {
        log.warn("Failed to lock " + fileName, ex);
      }
    }
    
  2. # 2 楼答案

    您打算将数据写入一个文件。因此,如果您试图锁定整个文件,最好使用一个线程来完成这项工作。虽然生成了20个线程,但每次调用该方法时只有一个线程正在运行,其他线程只是在等待锁定

    我建议您使用RandomAccessFile将数据写入文件。然后,每个线程都可以将一些唯一的数据写入文件,而无需锁定整个文件

    一些演示代码如下

    try {
        final RandomAccessFile file = new RandomAccessFile("/path/to/your/result.txt", "rw");
        final int numberOfThread = 20;
        final int bufferSize = 512;
        ExecutorService pool = Executors.newFixedThreadPool(numberOfThread);
        final AtomicInteger byteCounter = new AtomicInteger(0);
        final byte[] yourText = "Your data".getBytes();
        for (int i = 0; i < yourText.length; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    int start = byteCounter.getAndAdd(bufferSize);
                    int chunkSize = bufferSize;
                    if (start + bufferSize > yourText.length) {
                        chunkSize = yourText.length - start;
                    }
                    byte[] chunkData = new byte[chunkSize];
                    System.arraycopy(yourText, start, chunkData, 0, chunkSize);
                    try {
                        file.write(chunkData);
                    } catch (IOException e) {
                        //exception handle
                    }
                }
            });
        }
        file.close();
    } catch (Exception e) {
        //clean up
    }
    
  3. # 3 楼答案

    通过包含要写入文件的synchronized method的类访问文件。一次只有一个线程能够执行该方法

    我认为单例模式适合您的问题:

    package com.test.singleton;
    
    public class Singleton {
        private static final Singleton inst= new Singleton();
    
        private Singleton() {
            super();
        }
    
        public synchronized void writeToFile(String str) {
            // Do whatever
        }
    
        public Singleton getInstance() {
            return inst;
        }
    
    }
    

    每次需要写入文件时,您只需调用:

    Singleton.getInstance().writeToFile("Hello!!");