zoukankan      html  css  js  c++  java
  • 二、多线程的几种实现方式

    多线程三种主要实现方式:继承Thread类,实现Runnable接口、Callable和Futrue。

    一、简单实现

    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    import java.util.concurrent.TimeUnit;
    
    public class T02_HowToCreateThread {
        //1.继承Thread类
        static class MyThread extends Thread{
            @Override
            public void run() {
                System.out.println("MyThread-->");
            }
        }
        //3.实现Runnable接口
        static class MyRun implements Runnable{
    
            @Override
            public void run() {
                System.out.println("MyRunable-->");
            }
        }
        //4.实现Callable接口
        static class MyCall implements Callable{
    
            @Override
            public Object call() throws Exception {
                System.out.println("myCallable-->");
                return 1;
            }
        }
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            //1.继承Thread类
            new MyThread().start();
            //2.lambda与继承Thread类类//1.继承Thread类似,最简单
            new Thread(()->{
                System.out.println("lambda-->");
            }).start();
            //3.实现Runnable接口
            new Thread(new MyRun()).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("simple->Runnable");
                }
            }).start();
           //4.实现Callable接口,并用包装器FutureTask来同时实现Runable、Callable两个接口,可带返回结果
            MyCall mycall = new MyCall();
            FutureTask futureTask = new FutureTask(mycall);
            new Thread(futureTask).start();
            System.out.println(futureTask.get());
        }
    }

    二、使用ExecutorService、Callable和Future一起实现带返回值

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    
    /**
     * 使用ExecutorsService、Callable、Future来实现多个带返回值的线程
     */
    public class T02_HowToCreateThread02 {
        static class MyCallable implements Callable{
            private int taskNum;
    
            public MyCallable(int taskNum){
                this.taskNum = taskNum;
            }
            @Override
            public Object call() throws Exception {
                System.out.println("任务"+taskNum);
                return "MyCallable.call()-->task"+taskNum;
            }
        }
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            int num = 5;
            //创建一个线程池
            ExecutorService pool = Executors.newFixedThreadPool(num);
            List<Future> futureList = new ArrayList<Future>();
            for (int i = 0; i < num; i++){
                MyCallable mc = new MyCallable(i);
                //执行任务,并返回值
                Future future = pool.submit(mc);
                futureList.add(future);
            }
            pool.shutdown();
            for (Future f: futureList){
                System.out.println(f.get());
            }
        }
    }

    结果:

  • 相关阅读:
    005 HTML+CSS(Class027
    004 HTML+CSS(Class024
    003 HTML+CSS(Class011
    002HTML+CSS(class007-010)
    001HTML+CSS(class001-006)
    021 vue路由vue-router
    020 Vue 脚手架CLI的使用
    019 Vue webpack的使用
    018 vue的watch属性
    017 vue的插槽的使用
  • 原文地址:https://www.cnblogs.com/helq/p/13264050.html
Copyright © 2011-2022 走看看