为什么线程在单核CPU上工作? [英] Why is threading works on a single core CPU?

查看:113
本文介绍了为什么线程在单核CPU上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地理解Java中的线程,我编写了以下代码

To better understand Threading in Java, I wrote the following code

public class SimpleRunnableTest {
   public static void main(String[] args) throws InterruptedException {
       long start = System.currentTimeMillis();

       Thread t1 = new Thread(new TT1());
       t1.start();
       Thread t2 = new Thread(new TT2());
       t2.start();

       t2.join();
       t1.join();

       long end = System.currentTimeMillis();
       System.out.println("end-start="+(end-start));
       }
}
class TT1 implements Runnable {
    public void run(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class TT2 implements Runnable {
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}    

这个想法是,如果我在main线程中依次运行Thread.sleep(5000)Thread.sleep(1000),则消耗的时间将是6 sec,但是由于我使用的是线程技术,因此在多核CPU上仅花费5 sec机器,它做到了.但是我的问题是:

The idea is if I run Thread.sleep(5000) and Thread.sleep(1000) sequentially in main Thread, the time consumed would be 6 sec, but since I am using Threading, it would only cost 5 sec on a multicore CPU machine, and it did. But my question is:

为什么在单核CPU机器上结果仍为5 sec?当然使用了线程,但是它不只是通过时分复用来模拟线程吗?

Why is the result still 5 sec on a single core CPU machine? Of course Threading is used, but isn't it just simulated threading by time division multiplexing?

我对时分复用的理解是:假设Thread.sleep(5000)是任务A,而Thread.sleep(1000)是任务B,我们可以将其分解为:A1,A2,A3; B1,B2

My understanding of time division multiplexing is: assume Thread.sleep(5000) is task A, and Thread.sleep(1000) is task B, and we could break it apart to be : A1, A2, A3 ; B1, B2

顺序就是:A1,A2,A3,B1,B2

Sequential is just : A1, A2, A3, B1, B2

时分多路复用线程只是:A1,B1,A2,B2,A3

Time division Multiplexing Threading is just: A1, B1, A2, B2, A3

如果是,那么第一个花费6秒,而第二个仅花费5秒呢?

If it is, how come the first costs 6 seconds, and the 2nd only 5?

我是这里的基地吗?

推荐答案

结果为5,而不是6,因为两个线程可以同时进入睡眠状态.通过调用Thread.sleep()进入睡眠状态将使另一个线程运行,但是剩余的睡眠间隔计时器将继续在两个线程中计时.

The result is 5, not 6, because the two threads can sleep simultaneously. Going to sleep by calling Thread.sleep() lets the other thread run, but the remaining sleep interval timer continues ticking for both threads.

请注意,这仅适用于睡眠(使用接近零的CPU),而不适用于做有用的工作:在这种情况下,单核非超线程CPU上的计时确实是可加的.例如,如果一个线程需要进行五秒钟的数字处理,而另一个线程需要进行第二秒的数字处理,那么两个线程的总时间将为六秒.

Note that this applies only to sleeping (which uses nearly zero CPU), but not to doing useful work: in this case the timing on a single-core non-hyperthreaded CPU would indeed be additive. For example, if one thread needed to do five seconds worth of number crunching, and the other needed to do a second worth of number crunching, the total time for both threads would be six seconds.

这篇关于为什么线程在单核CPU上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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