Multi threaded single file writing in Java

Mike

This questions seems common, but I am facing issues while multi-threads are writing into the same file(Excel). Here is my code:

public class XLWriter {

private XLWriter() {
}

private static class SingletonHelper {
    private static final XLWriter INSTANCE = new XLWriter();
}

public static synchronized XLWriter getInstance() {
    return SingletonHelper.INSTANCE;
}

public static synchronized void writeOutput(Map<String, String> d) {
    try {
        --- Write file
    } catch (Exception e) {
        SOP("Not able to write output to the output file.");
    }
}

public static void createWorkBook(String fileName, String sheetName)
        throws IOException {
    try {
        -- Create workbook
    } catch (WriteException e) {
        System.out.println("Could not create workbook" + e);
    }
}

I am using testng framework and 10 threads try to write into the same file. Many of the threads are not able to write into it and goes inside the Exception block... What is the better way to do this? Any code samples will greatly help me as I have very less time to finish this.. Thank you.

TwoThe

You do not need to synchronize pure read, so public static synchronized XLWriter getInstance() can go without synchronized without any error. You only need to synchronize writes, where more than one thread could potentially write/read the same data at the same time.

There are two things that can be done to solve your problem. The easy one is to create a special write function which is then the only one to be synchronized:

private void write(final File f, final Map<String, String> output) {
  synchronized(f) {
    // do the write
  }
}

Synchronizing on f is correct, because this is the shared resource that has to be exclusively accessed.

You do not need to change the createWorkBook function as long as you tell the file creation to not overwrite existing files, because the file-system itself is already thread-safe. Now you just need a look-up table for files which are already opened to get the appropriate file handle, because there must be only one File per file opened at any time. This can be solved using a ConcurrentHashMap like this:

public void getFileFor(String filename) {
  File newFile = File(filename);
  File inList = fileHashMap.putIfAbsent(newFile);
  if (inList != null) {
    return inList;
  } else {
    return newFile;
  }
}

Just make sure that you have a solution somewhere to close all files in the hashmap once you are done.

For further ideas: you can as well create a single write-thread with the only purpose to write stuff into that file. If this thread has a ConcurrentBlockingQueue, other threads can add their part into this queue instead of the file, then proceed with whatever they were doing, while the write-thread is the only one who has access to the file ever. Therefore no write issues can arise, and the thread safety is completely handles in the ConcurrentBlockingQueue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multi threaded single file writing in Java

From Dev

Is Java single-threaded or multi-threaded by default?

From Dev

Is Java single-threaded or multi-threaded by default?

From Dev

Java / Scala multi thread file writing

From Dev

Java Multi-Threaded Processes

From Dev

multi threaded task scheduler in java

From Dev

Multi-Threaded program runs slower than single threaded

From Dev

How to write Kafka consumers - single threaded vs multi threaded

From Dev

Single vs Multi-threaded JMS Producer

From Dev

Executing multiple database calls - single or multi threaded?

From Dev

Single threaded Node.js on a multi core CPU vs Java (Tomcat)

From Dev

multi-threaded file transfer with socket

From Dev

Multi threaded reading from a file in c++?

From Dev

Integer assignment in multi-threaded environment java

From Dev

Java Singleton Class Behaviour in Multi Threaded environment

From Dev

Object Visibility in a Multi-threaded Program in Java

From Dev

java callback functions in a multi threaded environment

From Dev

Multi-Threaded Prime number Finder in Java

From Dev

Multi-threaded Java TCP Client

From Dev

How to share single SQLite connection in multi-threaded Python application

From Dev

CompletableFuture multi-threaded, single thread concurrent, or both?

From Dev

Single Threaded Event Loop vs Multi Threaded Non Blocking Worker in Node.JS

From Dev

Migrate a single threaded app to multi-threaded, parallel execution, monte carlo simulation

From Dev

Single Threaded Event Loop vs Multi Threaded Non Blocking Worker in Node.JS

From Dev

The performance comparison of multi-threaded inserts(updates) on database and single-threaded sequential inserts(updates)?

From Dev

Multi-threaded C# application log file locking issue

From Dev

Multi-threaded C# application log file locking issue

From Dev

How does a single CPU handle Multi-threaded and multi-process applications?

From Dev

How does a single CPU handle Multi-threaded and multi-process applications?

Related Related

  1. 1

    Multi threaded single file writing in Java

  2. 2

    Is Java single-threaded or multi-threaded by default?

  3. 3

    Is Java single-threaded or multi-threaded by default?

  4. 4

    Java / Scala multi thread file writing

  5. 5

    Java Multi-Threaded Processes

  6. 6

    multi threaded task scheduler in java

  7. 7

    Multi-Threaded program runs slower than single threaded

  8. 8

    How to write Kafka consumers - single threaded vs multi threaded

  9. 9

    Single vs Multi-threaded JMS Producer

  10. 10

    Executing multiple database calls - single or multi threaded?

  11. 11

    Single threaded Node.js on a multi core CPU vs Java (Tomcat)

  12. 12

    multi-threaded file transfer with socket

  13. 13

    Multi threaded reading from a file in c++?

  14. 14

    Integer assignment in multi-threaded environment java

  15. 15

    Java Singleton Class Behaviour in Multi Threaded environment

  16. 16

    Object Visibility in a Multi-threaded Program in Java

  17. 17

    java callback functions in a multi threaded environment

  18. 18

    Multi-Threaded Prime number Finder in Java

  19. 19

    Multi-threaded Java TCP Client

  20. 20

    How to share single SQLite connection in multi-threaded Python application

  21. 21

    CompletableFuture multi-threaded, single thread concurrent, or both?

  22. 22

    Single Threaded Event Loop vs Multi Threaded Non Blocking Worker in Node.JS

  23. 23

    Migrate a single threaded app to multi-threaded, parallel execution, monte carlo simulation

  24. 24

    Single Threaded Event Loop vs Multi Threaded Non Blocking Worker in Node.JS

  25. 25

    The performance comparison of multi-threaded inserts(updates) on database and single-threaded sequential inserts(updates)?

  26. 26

    Multi-threaded C# application log file locking issue

  27. 27

    Multi-threaded C# application log file locking issue

  28. 28

    How does a single CPU handle Multi-threaded and multi-process applications?

  29. 29

    How does a single CPU handle Multi-threaded and multi-process applications?

HotTag

Archive