java创建线程的4种方式--使用线程池

 

 

 

 

 

package com.LearnJava.Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class numThread2 implements Runnable{
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            if (i%2==0){
                System.out.println(i);
            }
        }
    }
}
class nT implements Callable{
    @Override
    public Object call() throws Exception {
        for(int i=0;i<100;i++){
            if(i%2!=0){
                System.out.println(i);
            }
        }
        return null;
    }
}
public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        executorService.execute(new numThread2());
        //执行Callable方法
        executorService.submit(new nT());
        executorService.shutdown();

    }
}

 

posted @ 2020-02-11 09:45  超级学渣渣  阅读(490)  评论(0编辑  收藏  举报