Java multithreading in notebook computers with quad core processors

I'm reading a java tutorial that says that actual multithreading does not occur on a machine with a single processor It mentions that the operating system allocates a specified time for Java processes, and the JVM thread scheduler selects threads that run one thread at a time for a short duration

I have a laptop with a quad core processor – can I programmatically run multithreaded programs faster by running a thread on each core? The reason I ask this question is because it is mentioned in the book that only a real multiprocessor system can do multiple things at the same time

Solution

Even a single CPU can execute "multiple things" at the same time in a loose sense, but they are not really parallel You can start 100 threads to run on a single core, and they will get a time slice during which each thread can run some instructions, giving the impression that they all execute at the same time

As I said in another so post: multithreading on dual core machine?

The term thread usually contains three layers of abstraction:

>User threads are threads started by an application and map n: m to: > kernel threads, which are managed by the operating system, and N: m to: > hardware threads, which are available physical resources

Java threads are user threads The four cores in the CPU are counted as hardware threads Since the cross layer mapping is n: m, you can see that multiple user threads can be mapped to a smaller number of hardware threads

Now, having said this, there are usually two types of thread activities, each with its own quirks:

>I / O threads: these threads spend most of their time waiting for read / write operations from the stream and are blocked during this period (they will not be scheduled for execution until an event occurs) There are bright spots on the CPU, which can run simultaneously even on a single core. > Computing threads: these threads perform a large number of numerical operations and maximize the use of CPU Generally, starting more than (twice the number of available cores) such threads will reduce performance, because the CPU has a limited number of functional units: ALU, FPU, etc

The second type of thread above allows you to really see benefits on a quad core CPU or run multithreaded Java programs The following is a simple example of a program that first executes 1.000 in sequence 000.000 digits squared, and then executed in parallel using a thread pool of 4 threads:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class ThreadTask implements Runnable {

    private int total = 0;

    public ThreadTask(int total) {
        this.total = total;
    }

    @Override
    public void run() {
        int value = 0;
        for(int i = 0; i < total; i++) {
            value = i * i;
        }
    }       
}

public class Test {

    public static void main(String[] args) throws InterruptedException {

        int total = 1000000000;

        long start = System.currentTimeMillis();
        long value = 0;
        for(int i = 0; i < total; i++) {
            value = i * i;
        }       
        long stop = System.currentTimeMillis();

        System.out.println((stop - start) + " ms");

        ExecutorService exec = Executors.newFixedThreadPool(4);
        start = System.currentTimeMillis();
        for(int i = 0; i < 4; i++) {
            exec.submit(new ThreadTask(total / 4));
        }
        exec.shutdown();
        exec.awaitTermination(10,TimeUnit.SECONDS);
        stop = System.currentTimeMillis();

        System.out.println((stop - start) + " ms");     
    }
}

If the running speed is too fast, please adjust the total value at will Now I'm using Intel Lexus netbook, so it's not fast

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>