• 多线程的三种实现


    1.继承Thread类,重写run方法

    public class MyThread extends Thread{
        //继承Thread会产生单继承局限,使用Runnable比较好
        
        private String name;
        MyThread(String name){
            this.name = name;
        }
        @Override
        public void run() {
            for(int i=0;i<10;i++) {
                System.out.println(this.name+" "+i);
            }
        }
    }
    public static void main(String[] args) {
            MyThread t1 = new MyThread("thread1");
            MyThread t2 = new MyThread("thread2");
            MyThread t3 = new MyThread("thread3");
            t1.start();
            /*
            t1.start();  
            if (threadStatus != 0)//如果已經启动了,再次启动会抛这个异常
            throw new IllegalThreadStateException();
            */
            t2.start();
            t3.start();
            
            
        }

    2.实现Runnable接口

    public class MyThread implements Runnable{
        
        
        private String name;
        MyThread(String name){
            this.name = name;
        }
        @Override
        public void run() {
            for(int i=0;i<10;i++) {
                System.out.println(this.name+" "+i);
            }
        }
    }
        public static void main(String[] args) {
            MyThread t1 = new MyThread("thread");
            //MyThread t2 = new MyThread("thread2");
            //MyThread t3 = new MyThread("thread3");
            Thread thread1 = new Thread(t1);
            Thread thread2 = new Thread(t1);
            Thread thread3 = new Thread(t1);
            thread1.start();
            thread2.start();
            thread3.start();
            
            
    //        Runnable r = ()->{
    //            for(int i=0;i<10;i++) {
    //                System.out.println(i);
    //            }};
    //        Thread t1 = new Thread(r);
    //        t1.setName("thread1");
    //        Thread t2 = new Thread(r);
    //        t2.setName("thread1");
    //        Thread t3 = new Thread(r);
    //        t3.setName("thread1");
    //        t1.start();
    //        t2.start();
    //        t3.start();
            
        }

    3.实现Callable接口

    public class MyCallable implements Callable{
        //Callable接口比Runnable多了个返回值
        //模拟卖票
        int ticket = 10;
        @Override
        public String call() throws Exception {
            while(ticket > 0) {
                System.out.println("ticket has: "+ticket--);
            }
            return "ticket had sell done";
        }
    
    }
    public static void main(String[] args) throws InterruptedException, ExecutionException {
            Callable<String> cal = new MyCallable();
            FutureTask<String> task = new FutureTask<String>(cal);
            Thread thread1 = new Thread(task);
            Thread thread2 = new Thread(task);
            thread1.start();
            thread2.start();
            System.out.println(task.get());//取得返回值
            
            //以下是Lambda表达式实现
            /*
            Callable<String> cal2 =()->{
                for(int i=0;i<10;i++) {
                    System.out.println(i);
                }
                return "success";
            }; 
            FutureTask<String> task2 = new FutureTask<String>(cal2);
            Thread thread11 = new Thread(task2);
            Thread thread22 = new Thread(task2);
            thread11.start();
            thread22.start();
            System.out.println(task2.get());//取得返回值
            */
            
        }
    
    
    Callable接口的源码

    @FunctionalInterface
    public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; }
    总结:继承Thread会产生单继承局限,使用Runnable比较好,Callable接口比runnable接口多了个返回值
    但最终都是通过Thread类的start启动线程
  • 相关阅读:
    Kmeans中文聚类
    第四周周总结
    数据清洗第一天
    第三周周总结
    关于sqoop上传mysql数据到hive报错的问题
    天津东软实训第十一天——Hive连接JDBC
    天津东软实训第十天——Hive配置
    天津东软实训第九天——MapReduce实战
    天津东软实训第八天------倒排索引
    Intellij IDEA 创建maven项目,利用API操作HDFS
  • 原文地址:https://www.cnblogs.com/wwzyy/p/5531943.html
Copyright © 2020-2023  润新知