Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:29+00:00 2026-05-22T20:49:29+00:00

I have a program that performs lots of calculations and reports them to a

  • 0

I have a program that performs lots of calculations and reports them to a file frequently. I know that frequent write operations can slow a program down a lot, so to avoid it I’d like to have a second thread dedicated to the writing operations.

Right now I’m doing it with this class I wrote (the impatient can skip to the end of the question):

public class ParallelWriter implements Runnable {

    private File file;
    private BlockingQueue<Item> q;
    private int indentation;

    public ParallelWriter( File f ){
        file = f;
        q = new LinkedBlockingQueue<Item>();
        indentation = 0;
    }

    public ParallelWriter append( CharSequence str ){
        try {
            CharSeqItem item = new CharSeqItem();
            item.content = str;
            item.type = ItemType.CHARSEQ;
            q.put(item);
            return this;
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }

    public ParallelWriter newLine(){
        try {
            Item item = new Item();
            item.type = ItemType.NEWLINE;
            q.put(item);
            return this;
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }

    public void setIndent(int indentation) {
        try{
            IndentCommand item = new IndentCommand();
            item.type = ItemType.INDENT;
            item.indent = indentation;
            q.put(item);
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }

    public void end(){
        try {
            Item item = new Item();
            item.type = ItemType.POISON;
            q.put(item);
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }

    public void run() {

        BufferedWriter out = null;
        Item item = null;

        try{
            out = new BufferedWriter( new FileWriter( file ) );
            while( (item = q.take()).type != ItemType.POISON ){
                switch( item.type ){
                    case NEWLINE:
                        out.newLine();
                        for( int i = 0; i < indentation; i++ )
                            out.append("   ");
                        break;
                    case INDENT:
                        indentation = ((IndentCommand)item).indent;
                        break;
                    case CHARSEQ:
                        out.append( ((CharSeqItem)item).content );
                }
            }
        } catch (InterruptedException ex){
            throw new RuntimeException( ex );
        } catch  (IOException ex) {
            throw new RuntimeException( ex );
        } finally {
            if( out != null ) try {
                out.close();
            } catch (IOException ex) {
                throw new RuntimeException( ex );
            }
        }
    }

    private enum ItemType {
        CHARSEQ, NEWLINE, INDENT, POISON;
    }
    private static class Item {
        ItemType type;
    }
    private static class CharSeqItem extends Item {
        CharSequence content;
    }
    private static class IndentCommand extends Item {
        int indent;
    }
}

And then I use it by doing:

ParallelWriter w = new ParallelWriter( myFile );
new Thread(w).start();

/// Lots of
w.append(" things ").newLine();
w.setIndent(2);
w.newLine().append(" more things ");

/// and finally
w.end();

While this works perfectly well, I’m wondering:
Is there a better way to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:49:30+00:00Added an answer on May 22, 2026 at 8:49 pm

    Your basic approach looks fine. I would structure the code as follows:

        import java.io.BufferedWriter;
        import java.io.File;
        import java.io.IOException;
        import java.io.Writer;
        import java.util.concurrent.BlockingQueue;
        import java.util.concurrent.LinkedBlockingQueue;
        import java.util.concurrent.TimeUnit;
        
        public interface FileWriter {
            FileWriter append(CharSequence seq);
        
            FileWriter indent(int indent);
        
            void close();
        }
        
        class AsyncFileWriter implements FileWriter, Runnable {
            private final File file;
            private final Writer out;
            private final BlockingQueue<Item> queue = new LinkedBlockingQueue<Item>();
            private volatile boolean started = false;
            private volatile boolean stopped = false;
        
            public AsyncFileWriter(File file) throws IOException {
                this.file = file;
                this.out = new BufferedWriter(new java.io.FileWriter(file));
            }
        
            public FileWriter append(CharSequence seq) {
                if (!started) {
                    throw new IllegalStateException("open() call expected before append()");
                }
                try {
                    queue.put(new CharSeqItem(seq));
                } catch (InterruptedException ignored) {
                }
                return this;
            }
        
            public FileWriter indent(int indent) {
                if (!started) {
                    throw new IllegalStateException("open() call expected before append()");
                }
                try {
                    queue.put(new IndentItem(indent));
                } catch (InterruptedException ignored) {
                }
                return this;
            }
        
            public void open() {
                this.started = true;
                new Thread(this).start();
            }
        
            public void run() {
                while (!stopped) {
                    try {
                        Item item = queue.poll(100, TimeUnit.MICROSECONDS);
                        if (item != null) {
                            try {
                                item.write(out);
                            } catch (IOException logme) {
                            }
                        }
                    } catch (InterruptedException e) {
                    }
                }
                try {
                    out.close();
                } catch (IOException ignore) {
                }
            }
        
            public void close() {
                this.stopped = true;
            }
        
            private static interface Item {
                void write(Writer out) throws IOException;
            }
        
            private static class CharSeqItem implements Item {
                private final CharSequence sequence;
        
                public CharSeqItem(CharSequence sequence) {
                    this.sequence = sequence;
                }
        
                public void write(Writer out) throws IOException {
                    out.append(sequence);
                }
            }
        
            private static class IndentItem implements Item {
                private final int indent;
        
                public IndentItem(int indent) {
                    this.indent = indent;
                }
        
                public void write(Writer out) throws IOException {
                    for (int i = 0; i < indent; i++) {
                        out.append(" ");
                    }
                }
            }
        }
    

    If you do not want to write in a separate thread (maybe in a test?), you can have an implementation of FileWriter which calls append on the Writer in the caller thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a program that performs highly computationally intensive calculations. The program
I have a program that connects to an Oracle database and performs operations on
I have a program that performs some network IO that compiles a 32 bit
I have a program that loads a file (anywhere from 10MB to 5GB) a
I was given the assignment to create a program that performs basic set operations.
I converted a program from IDL into CUDA that performs some calculations on a
We have a C# program that performs some timing measurements. Unfortunately, the first time
I am writing a program that performs different string operations on every letter of
I have a python program that performs several independent and time consuming processes. The
I have a c++ program which performs one function. It loads a large data-file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.