java 多线程Callable和Runable执行顺序问题详解

毫无疑问 Runnable会进行异步执行,此处不多数,主要说明Callable的使用,看实例:

1、

public class ThreadTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService executor = Executors.newFixedThreadPool(4);

        MyTread m1 = new MyTread();

        Future f = executor.submit(m1);

        // System.out.println(f.get());

        executor.shutdown();

        System.out.println("主线程执行完了");

    }

 

}

 

class MyTread implements Callable<String> {

    @Override

    public String call() {

        try {

            System.out.println("线程调度:" + Thread.currentThread());

            TimeUnit.SECONDS.sleep(3);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        return "123";

    }

}

此程序虽然获取了call方法的返回值,但是没有做处理,所以主线程main和m1同时执行,执行结果如下:

主线程执行完了

线程调度:Thread[pool-1-thread-1,5,main]

 

2、

public class ThreadTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService executor = Executors.newFixedThreadPool(4);

        MyTread m1 = new MyTread();

        Future f = executor.submit(m1);

        System.out.println(f.get()); // 进行了输出

        executor.shutdown();

        System.out.println("主线程执行完了");

    }

 

}

 

class MyTread implements Callable<String> {

    @Override

    public String call() {

        try {

            System.out.println("线程调度:" + Thread.currentThread());

            TimeUnit.SECONDS.sleep(3);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        return "123";

    }

}

 

在2中,对m1中call方法的返回值在main中进行了处理(输出),所以在此种情况下,main需要等待m1执行完,再继续执行,执行结果如下

线程调度:Thread[pool-1-thread-1,5,main]

123

主线程执行完了

 

3、再看当主线程中同时启动两个由Callable生成的线程时

public class ThreadTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService executor = Executors.newFixedThreadPool(4);

        MyTread m1 = new MyTread();

        MyTread2 m2 = new MyTread2();

        Long time1 = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());

        Future f = executor.submit(m1);

        Future f2 = executor.submit(m2);

        System.out.println(f.get()); // 进行了输出m1

        System.out.println(f2.get()); // 进行了输出m2

        executor.shutdown();

        System.out.println("主线程执行完了");

        Long time2 = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());

        System.out.println("主线程等待了" + (time2 - time1) + "秒");

    }

 

}

 

class MyTread implements Callable<String> {

    @Override

    public String call() {

        try {

            System.out.println("线程调度:" + Thread.currentThread());

            TimeUnit.SECONDS.sleep(3);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        return "123";

    }

}

 

class MyTread2 implements Callable<String> {

    @Override

    public String call() {

        try {

            System.out.println("线程调度2:" + Thread.currentThread());

            TimeUnit.SECONDS.sleep(3);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        return "abc";

    }

}

当不对m1和m2做输出时,main和m1、m2并发执行,当对m1和m2中任意一个的返回值进行处理的时候,main需要等待,但是m1和m2之前仍然是并发执行,执行结果如下:

线程调度2:Thread[pool-1-thread-2,5,main]

线程调度:Thread[pool-1-thread-1,5,main]

123

abc

主线程执行完了

主线程等待了3秒


特别说明:尊重作者的劳动成果,转载请注明出处哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt125
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值