[ https://issues.apache.org/jira/browse/CSV-295?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17504582#comment-17504582 ]
Gary D. Gregory commented on CSV-295: ------------------------------------- There are 2 solutions, I'm not sure why a semaphore is needed. (1) Lock the printer as needed with synchronized(someCsvPrinterVar) or (2) Update Commons CSV with: {code:java} diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index 171bcff..9dcb95c 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -162,7 +162,7 @@ * @throws IOException * If an I/O error occurs */ - public void print(final Object value) throws IOException { + public synchronized void print(final Object value) throws IOException { format.print(value, appendable, newRecord); newRecord = false; } @@ -188,7 +188,7 @@ * @throws IOException * If an I/O error occurs */ - public void printComment(final String comment) throws IOException { + public synchronized void printComment(final String comment) throws IOException { if (comment == null || !format.isCommentMarkerSet()) { return; } @@ -226,7 +226,7 @@ * @throws SQLException If a database access error occurs or this method is called on a closed result set. * @since 1.9.0 */ - public void printHeaders(final ResultSet resultSet) throws IOException, SQLException { + public synchronized void printHeaders(final ResultSet resultSet) throws IOException, SQLException { printRecord((Object[]) format.builder().setHeader(resultSet).build().getHeader()); } @@ -236,7 +236,7 @@ * @throws IOException * If an I/O error occurs */ - public void println() throws IOException { + public synchronized void println() throws IOException { format.println(appendable); newRecord = true; } @@ -254,7 +254,7 @@ * @throws IOException * If an I/O error occurs */ - public void printRecord(final Iterable<?> values) throws IOException { + public synchronized void printRecord(final Iterable<?> values) throws IOException { for (final Object value : values) { print(value); } {code} > Support for parallelism in CSVPrinter > ------------------------------------- > > Key: CSV-295 > URL: https://issues.apache.org/jira/browse/CSV-295 > Project: Commons CSV > Issue Type: Improvement > Components: Printer > Affects Versions: 1.9.0 > Environment: > https://zio.dev/version-1.x/overview/overview_creating_effects#blocking-synchronous-side-effects > Reporter: Zimo Li > Priority: Major > > I am trying to write the result of network IO to a CSV file using Scala and > the ZIO library. The order of the rows does not matter, so I decided to use a > concurrency of 8. > Each thread calls {{{}CSVPrinter.printRecord{}}}, and this caused some rows > to intersect with others. Eventually, I decided to use a > [Semaphore|https://zio.dev/version-1.x/datatypes/concurrency/semaphore] to > fix it. > A locking mechanism for {{printRecord}} can be implemented just like the > underlying {{FileWriter}} or {{PrintWriter}}. -- This message was sent by Atlassian Jira (v8.20.1#820001)