Why are two Java streams (in some cases) more than twice as fast as one?

File: Example1.java

public class Example1 implements Runnable {

    public void run() {
        for(int i = 0; i < 100000000; i++) {
            int x = 5;
            x = x * 4;
            x = x % 3;
            x = x + 9000;
            x = x * 923;
        }
    }

    public static void task() {
        for(int i = 0; i < 100000000; i++) {
            int x = 5;
            x = x * 4;
            x = x % 3;
            x = x + 9000;
            x = x * 923;
        }
        for(int i = 0; i < 100000000; i++) {
            int x = 9;
            x = x * 2;
            x = x % 4;
            x = x + 3241;
            x = x * 472;
        }
    }

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();
            Example1.task();
            Example1.task();
            Example1.task();
            Example1.task();
            Example1.task();
        long stopTime = System.currentTimeMillis();
        long runTime = stopTime - startTime;
        System.out.println("Run time for one thread: " + runTime);


        startTime = System.Example1();
            (new Thread(new Example1())).start();
            (new Thread(new Example2())).start();
            (new Thread(new Example1())).start();
            (new Thread(new Example2())).start();
            (new Thread(new Example1())).start();
            (new Thread(new Example2())).start();
            (new Thread(new Example1())).start();
            (new Thread(new Example2())).start();
            (new Thread(new Example1())).start();
            (new Thread(new Example2())).start();
        stopTime = System.currentTimeMillis();
        runTime = stopTime - startTime;
        System.out.println("Run time for two threads: " + runTime);


    }

}

      

File: Example2.java

public class Example2 implements Runnable {

    public void run() {
        for(int i = 0; i < 100000000; i++) {
            int x = 9;
            x = x * 2;
            x = x % 4;
            x = x + 3241;
            x = x * 472;
        }        
    }
}

      

When I run this, it outputs:

Execution time for one thread: 1219

Execution time for two threads: 281

or something very close.

Why is there such a difference? Why is splitting it into two threads more than twice as fast as just running it directly?

+2


source to share


3 answers


You are not really waiting for the threads to finish.

After you start the thread, you must call .join () on it to wait for it to complete. What happens here is that all your threads start, and once the last one starts, you start it, and then calculate the stop time. This means your threads are still running in the background.

Edit: The reason the first one takes so long is because you are making a series of synchronous calls, creating a thread and starting it, creating an asynchronous task.



Edit 2: Here's a propex sequence diagram of what's going on in your first test: http://www.websequencediagrams.com/cgi-bin/cdraw?lz=TWFpbi0-RXhhbXBsZTE6IFRhc2sgc3RhcnRlZAphY3RpdmKACEZSLAjap-

Here is a sequence of the tissues that occurs in the second test: http://www.websequencediagrams.com/cgi-bin/cdraw?lz=TWFpbi0tPkFub255bW91cyBUaHJlYWQ6IFN0YXJ0IEV4YW1wbGUxLnRhc2soKQoACSYyAAEuAAFdAAGBOwCCPjoAgyIGPk1haW46ICJIb3cgbG9uZyBkaWQgdGhhdCB0YWtlPyIKAINmEC0AKwhUYXNrcyBiZWdpbiB0byBmaW5pc2guLi4gKHNvbWUgbWF5IGhhdmUgZW5kZWQgZWFybGllcikK&s=napkin

Edit 3: I just realized that the second sequence diagram is pointing all arrows to / same / flow. They are actually DIFFERENT streams, each call.

+19


source


The start of calling () on the thread returns immediately because it just terminates the thread. The thread itself will start running in the background some time later.



+2


source


This is what I get with your code adding connection to streams:

Execution time for one thread: 566

Runtime for two threads: 294

So the previous answers are correct.

EDIT: I added that joins this way. You can do it better, but it doesn't matter:

    Thread[] t = new Thread[10];
    (t[0] = new Thread(new Example1())).start();
    (t[1] = new Thread(new Example2())).start();
    (t[2] = new Thread(new Example1())).start();
    (t[3] = new Thread(new Example2())).start();
    (t[4] = new Thread(new Example1())).start();
    (t[5] = new Thread(new Example2())).start();
    (t[6] = new Thread(new Example1())).start();
    (t[7] = new Thread(new Example2())).start();
    (t[8] = new Thread(new Example1())).start();
    (t[9] = new Thread(new Example2())).start();

    for (Thread t1: t) {
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

      

You need to join each topic. However, you don't waste time waiting in join () because other threads are not blocking. If a thread has finished executing it before you have connected, you simply continue with the next thread.

Also, what does your last comment mean?

+1


source







All Articles