实现线程池的4种方法

1. 继承Thread类实现多线程


//继承Thread类实现多线程
public class ExtendsThread extends Thread{

    public ExtendsThread(){

    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread()+": "+i);
        }
    }

    public static void main(String[] args) {
        ExtendsThread extendsThread1 = new ExtendsThread();
        ExtendsThread extendsThread2 = new ExtendsThread();
        ExtendsThread extendsThread3 = new ExtendsThread();
        extendsThread1.start();
        extendsThread2.start();
        extendsThread3.start();
    }
}

2.覆写Runnable()接口实现多线程

//覆写Runnable()接口实现多线程
public class ImplementsRunnable implements Runnable{
    public static int count = 20;
    @Override
    public void run() {
        while (count > 0){
            try{
               Thread.sleep(200);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"-当前剩余:"+ count--);
        }
    }

    public static void main(String[] args) {
        ImplementsRunnable implementsRunnable = new ImplementsRunnable();
        Thread thread1 = new Thread(implementsRunnable,"线程1");
        Thread thread2 = new Thread(implementsRunnable,"线程2");
        Thread thread3 = new Thread(implementsRunnable,"线程3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

3. 覆写Callable接口实现多线程

4. 通过线程池启动多线程

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

//通过线程池启动多线程
public class PoolThread {
    public static void main(String[] args) {
        // 1.FixThreadPool(int n); 固定大小的线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 6; i++) {
            fixedThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 5; j++) {
                        System.out.println(Thread.currentThread().getName() + "---"+j);
                    }
                }
            });
        }
        fixedThreadPool.shutdown();

        // 2.SingleThreadPoolExecutor :单线程池
        ExecutorService singleThread = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++) {
            singleThread.submit(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 5; j++) {
                        System.out.println(Thread.currentThread().getName() + "---"+j);
                    }
                }
            });
        }
        singleThread.shutdown();

        // 3.CashedThreadPool(); 缓存线程池
        ExecutorService cachedThread = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            cachedThread.submit(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 5; j++) {
                        System.out.println(Thread.currentThread().getName() + "---"+j);
                    }
                }
            });
        }
        cachedThread.shutdown();
    }
}


版权声明:本文为sliver1836原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。