java Executor学习与理解

java Executor

未完待续

package abc.repeat.test;

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

public class ExecutorTest {

    public static void main(String[] args) throws IOException, InterruptedException {
        boolean useExecutor = false;
        int waitMillis = 1000;
        int taskNo = 10000;

        long startTime = System.currentTimeMillis();

        if (useExecutor) {
            ExecutorService service = Executors.newFixedThreadPool(1);
            //ExecutorService service = Executors.newFixedThreadPool(10);
            //ExecutorService service = Executors.newFixedThreadPool(100);
            for (int i = 0; i < taskNo; i++) {
                Runnable run = new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("thread start");
                    }
                };
                service.execute(run);
            }
            service.shutdown();
            service.awaitTermination(waitMillis, TimeUnit.MILLISECONDS);

        } else {
            for (int i = 0; i < taskNo; i++) {
                Runnable run = new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("thread start");
                    }
                };
                Thread t = new Thread(run);
                t.start();
                t.join(waitMillis);
            }
        }

        System.out.println("all thread complete");
        long endTime = System.currentTimeMillis();
        System.out.println(String.format("TimeElapse: %s", endTime - startTime));
    }
}

你可能感兴趣的:(编程)