相关疑难解决方法(0)

为什么线程可以在单核CPU上运行?

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

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();
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java multithreading multiplexing

4
推荐指数
1
解决办法
2257
查看次数

标签 统计

java ×1

multiplexing ×1

multithreading ×1