zoukankan      html  css  js  c++  java
  • 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,
  • 相关阅读:
    记一次java程序内存溢出问题
    js 对象数据观察者实现
    requirejs和seajs使用感受
    maven根据不同的运行环境,打包不同的配置文件
    Quartz .net 一直运行失败
    Sql2008R2 日志无法收缩解决方案
    win7 64位英文版 ado驱动
    KB4284826 远程桌面发生身份验证错误,要求的函数不受支持
    Delphi System.zip patch with ZIP64 and LZMA supports
    native excel 文件已经打开的判断
  • 原文地址:https://www.cnblogs.com/duanxz/p/5053419.html
Copyright © 2011-2022 走看看