• 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,
  • 相关阅读:
    使用helm管理复杂kubernetes应用
    helm repository 相关
    PSQLException: An I/O error occurred while sending to the backend.
    使用helm进行kubernetes包管理
    Slave作为其它Slave的Master时使用
    ext3是对ext2文件系统的一个扩展高性能日志文件系统
    ready是先执行的,load后执行,DOM文档的加载步骤
    jQuery上定义插件并重设插件构造函数
    在PHP与HTML混合输入的页面或者模板中就需要对PHP代码进行闭合
    decode 函数将字符串从某种编码转为 unicode 字符
  • 原文地址:https://www.cnblogs.com/duanxz/p/5053419.html
Copyright © 2020-2023  润新知