具有四核处理器的笔记本电脑中的Java多线程 [英] Java multithreading in a laptop having quad-core processor

查看:164
本文介绍了具有四核处理器的笔记本电脑中的Java多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Java教程,其中提到在具有单个处理器的计算机中不会发生实际的多线程.它提到了OS为Java进程分配了指定的时间,并且JVM线程调度程序选择了用于在短时间内一次运行一个线程的线程.

I was going through a Java tutorial where it was mentioned that actual multithreading doesn't happen in a machine having a single processor. It mentioned that OS allots a specified amount of time for the Java process and JVM thread scheduler picks up threads for running one thread at a time for a small amount of time.

我有一台笔记本电脑使用哪个四核处理器-是否可以通过在每个核中运行一个线程来更快地以编程方式运行多线程程序?我之所以问这个问题,是因为书中提到只有一个真正的多处理器系统才能同时执行多项操作.

I have a laptop which quadcore processor - it is possible to run a multi-threaded program faster programatically by running one thread in each core? The reason why I am asking this question is because the book mentioned that only a true multi processor system can do multiple things at the same time.

推荐答案

即使是单个CPU也可以从松散的意义上同时完成多个任务",但是它们并不是真正并行的.您可以启动100个线程在单个内核上运行,它们将获得时间片,在每个时间片中它们可以运行一些指令,从而给人以印象,它们都在同一时间执行.

Even a single CPU can do "multiple things at the same time" in a loose sense, but they are not truly in parallel. You can start 100 threads to run on a single core and they will get time slices during which each of them can run a few instructions, thus creating the impression that they are all executing at the same time.

正如我在另一篇SO帖子中所述:双核计算机上的多线程?

As I've said in another SO post: multithreading on dual core machine?

线程一词通常涵盖三个抽象层:

The term threads usually covers three abstraction layers:

  1. 用户线程是由应用程序启动的线程,并且以N:M映射到:
  2. 内核线程,它们是由操作系统管理的线程,将N:M映射到:
  3. 硬件线程,它们是可用的实际物理资源.
  1. User threads are threads launched by applications and are mapped N:M to:
  2. Kernel threads, which are threads managed by the operating system, mapped N:M to:
  3. Hardware threads, which are the actual physical resources available.

Java线程是用户线程. CPU中的4个核心被视为硬件线程.由于跨层的映射是N:M,因此可以看到可以将多个用户线程映射到较少数量的硬件线程.

Java threads are user threads. The 4 cores in your CPU count as hardware threads. Since the mapping is N:M across the layers, you can see that you can have several user threads mapped to a smaller number of hardware threads.

现在,话虽如此,通常有两类线程活动,每种都有自己的怪癖:

Now, having said this, there are generally two classes of thread activities, each with their own quirks:

  1. I/O线程:这些线程大部分时间都在等待流中的读/写操作,并且在此期间被阻塞(在发生事件唤醒之前,它们不安排执行他们). CPU上有指示灯,即使在单个内核上,它们中的许多也可以同时运行.
  2. 计算线程:这些线程进行了大量的数字运算,并最大程度地使用CPU.由于CPU具有有限数量的功能单元:ALU,FPU等,通常启动的线程数(大于可用内核数的2倍)会降低性能.
  1. I/O threads: these threads spend most of their time waiting on read/write operations from a stream and are blocked in the meantime (they are not scheduled for execution until an event occurs to wake them up). There are light on the CPU and a lot of them can run concurrently even on a single core.
  2. Computational threads: these thread do a lot of number crunching and use the CPU to the maximum. Generally starting more than (2x the number of available cores) such threads is going to degrade performance, because the CPU has a limited number of functional units: ALUs, FPUs, etc.

上面的第二类线程使您真正看到了好处,或者在四核CPU上运行了多线程Java程序.这是一个简单的程序示例,该程序先按顺序执行1.000.000.000数字的平方,然后使用具有4个线程的线程池并行执行:

The second class of threads above lets you really see the benefit or running a multithreaded java program on your quad-core CPU. Here is a simple example of a program that executes squaring of 1.000.000.000 numbers first sequentially and then in parallel using a thread pool with 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");     
    }
}

如果total的运行速度太快,可以随意调整它的值.现在,我正在与Intel Atom一起从事上网本的开发,因此速度并不是很快.

Feel free to adjust the value of total if it's running too fast. Now I'm working on a netbook with Intel Atom, so it not really fast.

这篇关于具有四核处理器的笔记本电脑中的Java多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆