多线程--->线程的几种基本实现

import java.util.concurrent.*;

public class ThreadTest {
    public static void main(String[] args) {
        //1、继承Thread类
       /* MyThread myThread = new MyThread();
        myThread.start();
        System.out.println("主线程");*/
        /*
         * 运行结果:
         * 主线程
         * 执行子线程
         * 当然,这里的结果不代表线程的执行顺序,线程是并发执行的,如果多运行几次,打印顺序可能会不一样。多线程的运行过程中,CPU是以不确定的方式去执行线程的,故运行结果与代码的执行顺序或者调用顺序无关,运行结果也可能不一样。关于线程执行的随机性本文后面也有代码示例。
         * 这里还有一个需要注意的点就是main方法中应该调用的是myThread的start方法,而不是run()方法。调用start()方法是告诉CPU此线程已经准备就绪可以执行,进而系统有时间就会来执行其run()方法。而直接调用run()方法,则不是异步执行,而是等同于调用函数般按顺序同步执行,这就失去了多线程的意义了。
         */
        //2.实现Runnable接口
        /*Runnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println("主线程。。");*/
        /*
         *运行结果
         *主线程。。
         *执行子线程
         * 真正创建新线程还是通过Thread创建:
         * Thread thread = new Thread(runnable); 这一步Thread类的作用就是把run()方法包装成线程执行体,然后依然通过start去告诉系统这个线程已经准备好了可以安排执行。
         */
       // 3、使用Callable和Future创建线程
            //上面的两种方式都有这两个问题:
            //a.    无法获取子线程的返回值
            //b.    run方法不可以抛出异常
       /* Callable callable = new MyCallable();
        for (int i = 0; i < 5 ; i++) {
            FutureTask task = new FutureTask(callable);
            new Thread(task,"子线程"+i).start();
            try{
                //获取返回子线程的值
                System.out.println("子线程的返回值:"+task.get()+"
");
            }catch (Exception e){
                e.printStackTrace();
            }
        }*/
        /*
            子线程0i的值==0
            子线程的返回值:0

            子线程1i的值==1
            子线程的返回值:1

            子线程2i的值==2
            子线程的返回值:2

            子线程3i的值==3
            子线程的返回值:3

            子线程4i的值==4
            子线程的返回值:4

            如何启动线程?
            (1)创建一个Callable接口的实现类的对象
            (2)创建一个FutureTask对象,传入Callable类型的参数
            (3)调用Thread类重载的参数为Runnable的构造器创建Thread对象
            (4)取返回值调用FutureTask类的get()方法
         */
        //4.线程池 Executors类
        //  Executor    负责现成的使用和调度的根接口
        // *    |--ExecutorService    线程池的主要接口
        // *          |--ThreadPoolExecutor    线程池的实现类
        // *          |--ScheduledExecutorService    接口,负责线程的调度
        // *              |--ScheduledThreadPoolExecutor    (extends ThreadPoolExecutor implements ScheduledExecutorService)
        //使用Executors工具类中的方法创建线程池
       /* ExecutorService executorService = Executors.newFixedThreadPool(5);
        MyThreadPool myThreadPool = new MyThreadPool();
        //为线程池中的线程分配任务,使用submit方法,传入的参数可以是Runnable的实现类,也可以是Callable的实现类
        for (int i = 0; i < 5; i++) {
            executorService.submit(myThreadPool);
        }
        //关闭线程池
        //shutdown : 以一种平和的方式关闭线程池,在关闭线程池之前,会等待线程池中的所有的任务都结束,不在接受新任务
        //shutdownNow : 立即关闭线程池
        executorService.shutdown();*/

        /*
        pool-1-thread-4-------0
        pool-1-thread-3-------2
        pool-1-thread-3-------5
        pool-1-thread-3-------6
        pool-1-thread-1-------1
        pool-1-thread-2-------8
        pool-1-thread-2-------10
        pool-1-thread-2-------11
        pool-1-thread-2-------12
        pool-1-thread-2-------13
        pool-1-thread-2-------14
        pool-1-thread-2-------15
        pool-1-thread-2-------16
        pool-1-thread-2-------17
        pool-1-thread-2-------18
        pool-1-thread-3-------7
        pool-1-thread-3-------20
        pool-1-thread-4-------3
        pool-1-thread-5-------4
        pool-1-thread-5-------23
        pool-1-thread-5-------24
        pool-1-thread-5-------25
        pool-1-thread-5-------26
        pool-1-thread-5-------27
        pool-1-thread-5-------28
        pool-1-thread-5-------29
        pool-1-thread-5-------30
        pool-1-thread-5-------31
        pool-1-thread-5-------32
        pool-1-thread-5-------33
        pool-1-thread-5-------34
        pool-1-thread-5-------35
        pool-1-thread-5-------36
        pool-1-thread-5-------37
        pool-1-thread-5-------38
        pool-1-thread-5-------39
        pool-1-thread-5-------40
        pool-1-thread-5-------41
        pool-1-thread-5-------42
        pool-1-thread-5-------43
        pool-1-thread-5-------44
        pool-1-thread-5-------45
        pool-1-thread-5-------46
        pool-1-thread-5-------47
        pool-1-thread-4-------22
        pool-1-thread-3-------21
        pool-1-thread-2-------19
        pool-1-thread-1-------9
        pool-1-thread-4-------49
        pool-1-thread-5-------48
         */

       ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i <5 ; i++) {
            Future<Integer> future = executorService.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int result = 0;
                    for (int j = 0; j < 10; j++) {
                        result += j;
                    }
                    return result;
                }
            });

            try {
                System.out.println(Thread.currentThread().getName()+"-----"+future.get());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        executorService.shutdown();
        /*
        main-----45
        main-----45
        main-----45
        main-----45
        main-----45
         */

    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        super.run();
        System.out.println("执行子线程");
    }
}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println("执行子线程");
    }
}

class MyCallable implements Callable{
    int i = 0;
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"i的值=="+i);
        return i++;//call方法有返回值
    }
}
class  MyThreadPool implements Runnable{
    int i = 0;
    @Override
    public void run() {
        while(i<50){
            System.out.println(Thread.currentThread().getName()+"-------"+i++);
        }
    }
}
原文地址:https://www.cnblogs.com/cai170221/p/13651811.html