for循环创建多线程同时调用接口

**

多线程创建并同时调用接口

  1. for 创建 runnable 方式
 public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        if (Thread.currentThread().getName().equals("Thread-31")) {
                            throw new Throwable();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (Throwable throwable) {
                        System.out.println("error  " + Thread.currentThread().getName());
                        throwable.printStackTrace();
                    }
                    System.out.println("aaaaa" + Thread.currentThread().getName());
                }
            }).start();
        }
    }

多线程运行过程中如果有一个线程抛出异常,将不会影响其他线程运行。
当你循环调用接口的时候就可以实现高性能调用,互不影响。
**

2.callable 方法 for 循环创建

 public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            Callable<String> callable = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    Thread.sleep(1000);
                    if (Thread.currentThread().getName().equals("Thread-65")) {
                        try {
                            throw new Throwable();
                        } catch (Throwable throwable) {
                            System.err.println("currentThread:" + Thread.currentThread().getName());
                            throwable.printStackTrace();
                        }
                    }
                    System.err.println("aaaa" + Thread.currentThread().getName());
                    return null;
                }
            };
            FutureTask futureTask = new FutureTask(callable);
            try {
                new Thread(futureTask).start();
                //callable.call();  获取返回  多线程就不能同时执行。
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

需要注意的是如果要拿到返回结果。多个线程就不能同时运行,而是排队一个一个运行。改造方法如下3:

  1. 线程池方法运行线程并获取反回的结果;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //定义一个线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 6000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(60000));
        //创建自定义的一个对象
        MyCallable myCallable = new MyCallable();
        //创建集合用于存储任务
        List<Future> futures = new ArrayList<>();
        try {
            //循环提交任务到线程池中
            for (int i = 0; i < 10; i++) {
                Future<String> future = executor.submit(myCallable);
                futures.add(future);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("err");
        }

        //循环调用创建的任务
        for (Future<String> future : futures) {
            String s = future.get();
            //获取返回的结果
            System.out.println("result; " + s);
        }
        //关闭线程池的流
        executor.shutdown();
    }
}

/**
 * 自定义一个callable 的类
 */
class MyCallable implements Callable<String> {

    @Override
    public String call() {
        try {
            Thread.sleep(1000);

            if (Thread.currentThread().getName().equals("pool-1-thread-9")) {
                try {
                    throw new Throwable();
                } catch (Throwable throwable) {
                    System.err.println("error===== " + Thread.currentThread().getName());
                    throwable.printStackTrace();
                }
            }
            System.out.println("===wo de xian cheng" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            System.err.println("error===== " + Thread.currentThread().getName());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("error===== " + Thread.currentThread().getName());
            e.printStackTrace();
        }
        //返回线程调用的结果
        return "ok" + Thread.currentThread().getName();
    }

}

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值