关于java:线程顺序执行测试

43次阅读

共计 1685 个字符,预计需要花费 5 分钟才能阅读完成。

测试小案例:实现线程的程序执行
计划:(1) 应用 countdownlatch 形式
(2) 应用 thread 与 join 形式实现

第一种计划

package com.high.current08;

import java.util.concurrent.CountDownLatch;

public class OrderThread1 extends Thread {

    private CountDownLatch latch;

    public OrderThread1(CountDownLatch latch) {this.latch = latch;}

    @Override
    public void run() {System.out.println("Thread:" + Thread.currentThread().getName() + "is running");
        try {Thread.sleep(5000);
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println("Thread:" + Thread.currentThread().getName() + "is end");
        latch.countDown();}
}
package com.high.current08;

import java.util.concurrent.CountDownLatch;

public class TestOrder1 {public static void main(String[] args) throws InterruptedException {
        int number = 3;
        CountDownLatch latch =  new CountDownLatch(number);
        long t1=System.currentTimeMillis();
        for(int i=0;i<3;i++){OrderThread1 demo = new OrderThread1(latch);
            demo.start();}
        latch.await();
        System.out.println("main is end,and it spent"+(System.currentTimeMillis()-t1)+"ms");
    }
}

第二种计划

package com.high.current08;

public class OrderThread2 extends Thread{

    @Override
    public void run(){System.out.println("Thread:"+Thread.currentThread().getName()+"is running");
        try {Thread.sleep(5000);
        } catch (InterruptedException e) {e.printStackTrace();
        }
        System.out.println("Thread:"+Thread.currentThread().getName()+"is end");
    }
}
package com.high.current08;

public class TestOrder2 {public static void main(String[] args) throws InterruptedException {OrderThread2 thread1 = new OrderThread2();
        OrderThread2 thread2 = new OrderThread2();
        OrderThread2 thread3 = new OrderThread2();
        long t1=System.currentTimeMillis();
        thread1.start();
        thread2.start();
        thread3.start();

        thread1.join();
        thread2.join();
        thread3.join();
        System.out.println("main is end,and it spent"+(System.currentTimeMillis()-t1)+"ms");
     }
}

GITHUB

正文完
 0