码迷,mamicode.com
首页 > 其他好文 > 详细

Stream系列(十四)parallet方法使用

时间:2019-12-07 21:45:17      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:www   ace   oid   long   nal   sleep   cep   parallel   http   

并发

视频讲解:https://www.bilibili.com/video/av78408286/

技术图片

EmployeeTestCase.java
package com.example.demo;

import lombok.extern.log4j.Log4j2;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

@Log4j2
public class EmployeeTestCase extends BaseTestCase {
    @Test
    public void parallel(){
        list.stream().parallel().forEach(employee -> {
            cal(employee);
        });
    }
    private static void cal(Employee employee) {
        try {
            long lTime = Double.valueOf(employee.getSalary()).longValue();
            TimeUnit.MILLISECONDS.sleep(lTime);
            log.info("name:{}", employee.getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
BaseTestCase.java
package com.example.demo;

import java.util.Arrays;
import java.util.List;

public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

结果:

20:24:15.885 [ForkJoinPool.commonPool-worker-7] INFO com.example.demo.EmployeeTestCase - name:Alex
20:24:16.377 [main] INFO com.example.demo.EmployeeTestCase - name:Jack
20:24:16.378 [ForkJoinPool.commonPool-worker-9] INFO com.example.demo.EmployeeTestCase - name:Owen
20:24:16.878 [ForkJoinPool.commonPool-worker-3] INFO com.example.demo.EmployeeTestCase - name:Michael
20:24:16.878 [ForkJoinPool.commonPool-worker-5] INFO com.example.demo.EmployeeTestCase - name:Denny

关注公众号,坚持每天3分钟视频学习

技术图片

 

 

 

Stream系列(十四)parallet方法使用

标签:www   ace   oid   long   nal   sleep   cep   parallel   http   

原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12003249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!