java多线程参数传递,获取返回值(Callable)

 

import java.util.concurrent.*;

/**
 * 描述,构造方法传递参数,没有返回值的情况
 *
 */

public class Test implements Runnable{
    private static ThreadPoolExecutor threadPool
            = new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());
    private int num;

    public void method(int num) {
        for (int i = 0; i < 40; i++) {
            threadPool.execute(new Test(num));
        }


    }

    public Test(int num) {
        this.num = num;
    }
    public Test() {

    }

    @Override
    public void run() {
        try {
            Thread.currentThread().sleep(5000);
            System.out.println("核心线程数" + threadPool.getCorePoolSize());
            System.out.println("最大线程数" + threadPool.getMaximumPoolSize());
            System.out.println("线程池数" + threadPool.getPoolSize());
            System.out.println("队列任务数" + threadPool.getQueue().size());
            System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Test().method(123);

    }
}

 

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 描述,成员属性set方法传递参数,没有返回值
 *
 */

public class Test2 implements Runnable{
    private static ThreadPoolExecutor threadPool
            = new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());

    public void setNum(int num) {
        this.num = num;
    }

    private int num;

    public void method(int num) {
        for (int i = 0; i < 16; i++) {
            Test2 x = new Test2();
            x.setNum(num);
            threadPool.execute(x);
        }


    }

    @Override
    public void run() {
        person();
    }

    public void person(){
        try {
            //Thread.currentThread().sleep(5000);
            System.out.println("核心线程数" + threadPool.getCorePoolSize());
            System.out.println("最大线程数" + threadPool.getMaximumPoolSize());
            System.out.println("线程池数" + threadPool.getPoolSize());
            System.out.println("队列任务数" + threadPool.getQueue().size());
            System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Test2().method(123);

    }
}

 

import java.util.concurrent.*;

/**
 * 描述,实现Callable接口获取返回值
 *
 */

public class Test3 implements Callable {
    private static ThreadPoolExecutor threadPool
            = new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());

    public void setNum(int num) {
        this.num = num;
    }

    private int num;

    public String method(int num){
        String s = "";
        Test3 x = new Test3();
        x.setNum(num);
        FutureTask<String> thread = new FutureTask<String>(x);
        threadPool.submit(thread);
        try {

            s = thread.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        return s;
    }

    @Override
    public String call() throws Exception {
        return person();

    }

    public String person(){
        try {
            System.out.println("核心线程数" + threadPool.getCorePoolSize());
            System.out.println("最大线程数" + threadPool.getMaximumPoolSize());
            System.out.println("线程池数" + threadPool.getPoolSize());
            System.out.println("队列任务数" + threadPool.getQueue().size());
            System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "dsasd";
    }

    public static void main(String[] args) {
        for (int i = 0; i < 16; i++) {
            String s = new Test3().method(123);
            System.out.println(s+"    fgdgfdgsgfdgdgds");
        }


    }
}

 

import java.util.concurrent.*;

/**
 * 描述,匿名内部类方式的获取返回值和传递参数
 *
 */

public class Test4 {
    private static ThreadPoolExecutor threadPool
            = new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());

    public String method(int num){
        FutureTask<String> thread = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return person(num);
            }
        });
        threadPool.submit(thread);
        String s = "";
        try {
            s = thread.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return s;
    }


    public String person(int num){
        try {
            Thread.sleep(5000);
            System.out.println("2核心线程数" + threadPool.getCorePoolSize());
            System.out.println("2最大线程数" + threadPool.getMaximumPoolSize());
            System.out.println("2线程池数" + threadPool.getPoolSize());
            System.out.println("2队列任务数" + threadPool.getQueue().size());
            System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "dsasd";
    }


    public static void main(String[] args) {
        for (int i = 0; i < 16; i++) {
            String s = new Test4().method(123);
            System.out.println(s+"    fgdgfdgsgfdgdgds");
        }


    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值