创建多线程的方式(后两种)

/*
创建多线程的方式四:线程池
好处:便于线程管理
corePoolSize:核心池的大小
maximumPoolSize:最大线程数
keepAliveTime:线程没有任务时最多保持多长时间会终止

 */
class NewThread implements Callable {
    int sum = 0;

    @Override
    public Object call() throws Exception {
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                sum += i;
            }
        }
        return sum;
    }
}
class NewThread1 implements Callable {
    int sum = 0;

    @Override
    public Object call() throws Exception {
        for (int i = 1; i <= 100; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                sum += i;
            }
        }
        return sum;
    }
}
public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
    //设置线程池的属性
//        service1.setCorePoolSize(15);
//        service1.setMaximumPoolSize(20);
        System.out.println(service.getClass());
        FutureTask futureTask = new FutureTask(new NewThread());
        FutureTask futureTask1 = new FutureTask(new NewThread1());

        //        service.execute();
        service.submit(new NewThread());   //适用于callable接口
        service.submit(new NewThread1());
        try {
            System.out.println("总和为:" + futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        try {
            System.out.println("总和为:" + futureTask1.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        service.shutdown();


    }
}
//jdk5.0新增两种多线程的创建方式
    /*
    实现Callable接口:需要使用Future接口,Future接口有唯一实现类Future Task
    创建线程池
     */
//创建多线程的方式三:实现Callable接口。 ----JDK 5.0 新增
//    必须创建FutureTask对象
//如何理解实现Callable接口的方式创建多线程比实现Runnable接口创建多线程方式强大
    //(1)call方法可以有返回值
    //(2)call方法可以抛出异常

class Number implements Callable {

    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                sum += i;
            }
        }
        return sum;
    }
}

public class ThreadNew {
    public static void main(String[] args) {
        Number number = new Number();
        //将此callable接口实现类的对象作为传递到FutureTask构造器中,创建FutureTask的对象
        FutureTask futureTask = new FutureTask(number);
        Thread t1 = new Thread(futureTask);
        t1.setName("线程一:");
        t1.start();
        try {
            Object sum = futureTask.get();
            System.out.println("总和为:" + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

给TA打赏
共{{data.count}}人
人已打赏
知识碎片

网站项目中版心的介绍及使用

2021-11-29 20:55:19

知识碎片

CSS的书写顺序和规范

2021-11-29 21:20:07

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧