B
B
Bonce2020-05-28 11:31:31
Java
Bonce, 2020-05-28 11:31:31

Why does the multi-threaded version of the program take longer to run?

Hello! I'm learning multithreading in Java. I solve a simple problem: there is an input stream, you need to duplicate it in 2 different output streams. My decision:

private static class Tee {
        private BufferedReader reader;
        private PrintWriter fWriter;
        private PrintWriter sWriter;
        boolean done;
        String buffer;
        int readCount;
        int firstWriteCount;
        int secondWriteCount;

        public Tee(BufferedReader reader, PrintWriter fWriter, PrintWriter sWriter) {
            this.reader = reader;
            this.fWriter = fWriter;
            this.sWriter = sWriter;
        }

        public void teeWhileInsideCo() throws InterruptedException {
            Thread reader = new Thread(new LineReader());
            Thread fWriter = new Thread(new LineWriter(this.fWriter, 0));
            Thread sWriter = new Thread(new LineWriter(this.sWriter, 1));

            reader.start();
            fWriter.start();
            sWriter.start();

            reader.join();
            fWriter.join();
            sWriter.join();
        }

        private class LineReader implements Runnable {

            @Override
            public void run() {
                String line;
                while (true) {
                    try {
                        line = reader.readLine();
                    } catch (IOException e) {
                        line = null;
                    }

                    while (readCount != firstWriteCount || readCount != secondWriteCount) {
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    if (line == null) {
                        done = true;
                        break;
                    }

                    buffer = line;
                    readCount++;
                }
            }
        }

        private class LineWriter implements Runnable {
            private PrintWriter writer;
            private int number;

            public LineWriter(PrintWriter writer, int n) {
                System.out.println(Tee.this);
                this.writer = writer;
                this.number = n;
            }

            @Override
            public void run() {
                while (true) {
                    while (!done && ((number == 0 ? firstWriteCount : secondWriteCount) == readCount)) {
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (done) {
                        break;
                    }

                    writer.println(buffer);
                    if (number == 0) {
                        firstWriteCount++;
                    } else {
                        secondWriteCount++;
                    }
                }
                writer.flush();
            }
        }
    }


On test data, this version runs 30 times longer than the serial version of the program. Tell me what am I doing wrong? How exactly should you shoot so as not to hit yourself in the foot?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question