Multithreading is not necessarily faster than single-threaded

Multi-thread seems to have given us the impression that multi-threaded faster than single-threaded, in fact, this is a false proposition. Things are no absolutes, multi-threaded sometimes do faster than single-threaded, but many times there is no single-threaded so fast.  First simple distinguish between concurrency (concurrency) and parallelism (parallel). parallel to say the same time a number of commands executed simultaneously on multiple processors. concurrent mean only one instruction is executed at a time, but the process (thread) command CPU fast rotation, fast, giving the look is "run" the impression, in fact, only one instruction at a time.  but if we actually used in a multi-threaded application, the rotation between threads and context switching is required to spend a lot of time, so when we perform an operation similar to the circulation and the like, is not bound to a single-threaded multi-threaded means it fast (because there is no single thread of execution thread switching time consuming ), look at the following piece of code (from << Java concurrent programming art >>)

public class ConcurrencyTest {
    private static final long count = 1000000000;

    public static void main(String[] args) {
        try {
            concurrency();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        serial();
    }

    private static void concurrency() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                 int a = 0;
                 for (long i = 0; i < count; i++)
                 {
                     a += 5;
                 }
            }
        });
        thread.start();
        int b = 0;
        for (long i = 0; i < count; i++)
        {
            b--;
        }
        thread.join();
        long time = System.currentTimeMillis() - start;
        System.out.println("concurrency : " + time + "ms,b=" + b);
    }

    private static void serial() {
        long start = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < count; i++)
        {
            a += 5;
        }
        int b = 0;
        for (long i = 0; i < count; i++)
        {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("concurrency : " + time + "ms,b=" + b);
    }

}


The results of this code is run when the count value is relatively small when the single-threaded run faster than multi-threaded, when the count value is relatively large when multi-threaded speed will be about twice the single-threaded Why would this happen, in theory, this code is executing the operation does not require a single thread context switching should definitely faster than multithreaded fishes it.
 in fact, because we are good to be true, CPU resources are not give you all the Java program uses, other programs should take up CPU resources ah. For example, when we have not run this program the operating system may in fact have 50 threads running, then when we run this program, it will take up a single thread per CPU51 time 1 (assumed to be equal for each thread consumes time), multi-threaded CPU52 will take up two thirds of the time, what you say it will run faster. of course, when the rotation will take up between the relatively small number of relatively context of time more time so this time Although multithreading takes time CPU52 two thirds, but the switch also requires a lot of time so it is slower than single-threaded
.

Guess you like

Origin www.cnblogs.com/amiezhang/p/11285781.html