• Thread之四:java线程返回结果的方法


    两种方式:一种继承Thread类实现;一种通过实现Callable接口。

    第一种方法:

    因为实现Thread类的run方法自身是没有返回值的,所以不能直接获得线程的执行结果,但是可以通过在run方法里把最后的结果传递给实例变量,然后通过getXX方法获取该实例变量的值。继承实现的代码:

    package com.dxz.thread;
    
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    class RunThread extends Thread {
        private String runLog = "";
        private String name;
    
        public RunThread(String name) {
            this.name = name;
        }
    
        public void run() {
            try {
                int time = new Random().nextInt(10);
                System.out.println("sleep "+ time +" second.");
                TimeUnit.SECONDS.sleep(time);
                this.runLog = this.name + time;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public String getRunLog() {
            return this.runLog;
        }
    }
    package com.dxz.thread;
    public class RunThreadTest {
    
        public static void main(String[] args) throws InterruptedException {
            RunThread runT = new RunThread("world");
            runT.start();
            runT.join();
            System.out.println("result:="+runT.getRunLog());
    
        }
    
    }

    结果:

    sleep 3 second.
    result:=world3

    结果2:

    sleep 2 second.
    result:=world2

    第二种方法:

    继承Callable接口后需要实现call方法,而call方法默认是可以有返回值的,所以可以直接返回想返回的内容。接口的实现代码:

    package com.dxz.thread;
    
    import java.util.Random;
    import java.util.concurrent.Callable;
    import java.util.concurrent.TimeUnit;
    
    class CallThread implements Callable<String> {
        private String runLog = "";
        private String name;
    
        public CallThread(String name) {
            this.name = name;
        }
        @Override
        public String call() throws Exception {
            try {
                int time = new Random().nextInt(10);
                System.out.println("sleep " + time + " second.");
                TimeUnit.SECONDS.sleep(time);
                this.runLog = this.name + time;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return runLog;
        }
    }

    调用类:

    package com.dxz.thread;
    
    import java.util.ArrayList;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class CallThreadTest {
    
        public static void main(String[] args) throws ExecutionException {
            String result = "";
            ExecutorService exs = Executors.newCachedThreadPool();
            ArrayList<Future<String>> al = new ArrayList<Future<String>>();
            al.add(exs.submit(new CallThread("hello")));
            al.add(exs.submit(new CallThread("world")));
            for (Future<String> fs : al) {
                try {
                    result += fs.get() + ",";
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("result:="+result);
        }
    
    }

    结果:

    sleep 5 second.
    sleep 3 second.
    result:=hello3,world5,
  • 相关阅读:
    WSL2
    坐标系变换
    Python websocket
    PAJ7620 IIC 通信
    Python中assert的使用
    Python中循环的使用
    Linux 生成指定大小文件
    SVN不显示log 显示1970年问题
    阿里云 CS实例 开机自运行脚本文件
    生成UDS安全算法DLL文件
  • 原文地址:https://www.cnblogs.com/duanxz/p/5053419.html
Copyright © 2020-2023  润新知