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,
  • 相关阅读:
    hibernate的dialect大全
    jdbc.properties 链接各类数据库的基本配置以及URL写法
    Springboot中redis的学习练习
    博客开通了
    Java String类的hashCode()函数
    Java String类中CaseInsensitiveComparator.compare()方法的实现
    git pull远程所有分支
    Python的权限修饰符
    Tmux快捷键
    __future__模块
  • 原文地址:https://www.cnblogs.com/duanxz/p/5053419.html
Copyright © 2011-2022 走看看