java线程池newFixedThreadPool的创建和锁存器CountDownLatch的简单使用

创建实体类 Test

package com.test.pojo;

import java.io.Serializable;

public class Test implements Serializable {

    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Test() {
    }

    public Test(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

mybatis代码 逻辑层的代码就简单的插入数据方法 就不贴了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 配置命名空间,区别名称 -->
<mapper namespace="com.test.mapper.TestMapper">
    <insert id="addTest" parameterType="com.test.pojo.Test">
		insert into test_data(`name`,age) values (#{name},#{age})
	</insert>
</mapper>

线程池类

package com.test.util;

import com.test.pojo.Test;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPool {
    //数据
    private List<Test> list;

    // 锁存器 等于0时释放
    private CountDownLatch latch;

    // 线程池 参数10 是线程数量
    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    //计数
    private int a = 0;

    private final String url = "http://localhost:8080/test/addTest";
    // 创建任务
    public int work() {
        for (final Test temp : list) {
            // 执行多线程任务
            executorService.execute(new Runnable() {
                public void run() {
                    try {
                        RestTemplate restTemplate = new RestTemplate();
                        Integer result = restTemplate.postForObject(url, temp, Integer.class);
                        if (result > 0) {
                            a++;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        // 每走一次 锁存器就减1
                        latch.countDown();
                    }
                }
            });
        }
        try {
            // 如果锁存器的计数不等于0 所有线程不予通过
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return a;
    }


    public ThreadPool() {
    }

    public ThreadPool(List<Test> list, CountDownLatch latch) {
        this.list = list;
        this.latch = latch;
    }
}

控制器类

package com.test.controller;


import com.test.pojo.Test;
import com.test.service.TestService;
import com.test.util.ThreadPool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@RestController
//@Controller
@RequestMapping("/test")
// 跨域
@CrossOrigin
public class TestController {

    @Resource
    private TestService testService;

    @RequestMapping(value = "/threadPool", method = RequestMethod.GET)
    //@ResponseBody // 返回json格式的数据
    public String threadPool() {
        List<Test> list = new ArrayList<Test>();
        Test test1 = new Test(0, "小明", 20);
        Test test2 = new Test(0, "小红", 20);
        Test test3 = new Test(0, "小强", 20);
        Test test4 = new Test(0, "小敏", 20);
        Test test5 = new Test(0, "小懒", 20);
        Test test6 = new Test(0, "小虎", 20);
        list.add(test1);
        list.add(test2);
        list.add(test3);
        list.add(test4);
        list.add(test5);
        list.add(test6);
        // 锁存器
        CountDownLatch latch = new CountDownLatch(list.size());
        ThreadPool threadPool = new ThreadPool(list, latch);
        int a = threadPool.work();
        return "success:" + a + "\terror:" + (list.size() - a);
    }

    @RequestMapping(value = "/addTest", method = RequestMethod.POST)
    // @RequestBody JSON格式参数
    public int addTest(@RequestBody Test test) {
        int result = testService.addTest(test);
        return result;
    }
}

mysql数据库表

CREATE TABLE `test_data` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) CHARACTER SET gbk DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值