并发编程专题三-JAVA线程的并发工具类

代码如下:

import java.util.concurrent.ForkJoinPool;

import java.util.concurrent.RecursiveTask;

/**

  • @Auther: BlackKingW

  • @Date: 2019/4/14 12:09

  • @Description:

*/

public class SumArray {

private static class SumTask extends RecursiveTask{

private final static int THRESHOLD = MakeArray.ARRAY_LENGTH/10;

private int[] src; //表示我们要实际统计的数组

private int fromIndex;//开始统计的下标

private int toIndex;//统计到哪里结束的下标

public SumTask(int[] src, int fromIndex, int toIndex) {

this.src = src;

this.fromIndex = fromIndex;

this.toIndex = toIndex;

}

@Override

protected Integer compute() {

if(toIndex-fromIndex < THRESHOLD) {

int count = 0;

for(int i=fromIndex;i<=toIndex;i++) {

//SleepTools.ms(1);

count = count + src[i];

}

return count;

}else {

//fromIndex…mid…toIndex

//1…70…100

int mid = (fromIndex+toIndex)/2;

SumTask left = new SumTask(src,fromIndex,mid);

SumTask right = new SumTask(src,mid+1,toIndex);

invokeAll(left,right);

return left.join()+right.join();

}

}

}

public static void main(String[] args) {

ForkJoinPool pool = new ForkJoinPool();

int[] src = MakeArray.makeArray();

SumTask innerFind = new SumTask(src,0,src.length-1);

long start = System.currentTimeMillis();

pool.invoke(innerFind);//同步调用

System.out.println(“Task is Running…”);

System.out.println("The count is "+innerFind.join()

+" spend time:"+(System.currentTimeMillis()-start)+“ms”);

}

}

public class MakeArray {

//数组长度

public static final int ARRAY_LENGTH = 100000000;

public static int[] makeArray() {

//new一个随机数发生器

Random r = new Random();

int[] result = new int[ARRAY_LENGTH];

for(int i=0;i<ARRAY_LENGTH;i++){

//用随机数填充数组

result[i] = r.nextInt(ARRAY_LENGTH*3);

}

return result;

}

}

二、常用的并发工具类

==========

1、CountDownLatch

作用:是一组线程等待其他的线程完成工作以后在执行,加强版join

await用来等待,countDown负责计数器的减一

代码示例:

import java.util.concurrent.CountDownLatch;

/**

  • @Auther: BlackKingW

  • @Date: 2019/4/14 12:09

  • @Description:

*/

public class UseCountDownLatch {

static CountDownLatch latch = new CountDownLatch(6);

//初始化线程(只有一步,有4个)

private static class InitThread implements Runnable{

@Override

public void run() {

System.out.println(“Thread_”+Thread.currentThread().getId()

+" ready init work…");

latch.countDown();//初始化线程完成工作了,countDown方法只扣减一次;

for(int i =0;i<2;i++) {

System.out.println(“Thread_”+Thread.currentThread().getId()

+" …continue do its work");

}

}

}

//业务线程

private static class BusiThread implements Runnable{

@Override

public void run() {

try {

latch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

for(int i =0;i<3;i++) {

System.out.println(“BusiThread_”+Thread.currentThread().getId()

+" do business-----");

}

}

}

public static void main(String[] args) throws InterruptedException {

//单独的初始化线程,初始化分为2步,需要扣减两次

new Thread(new Runnable() {

@Override

public void run() {

SleepTools.ms(1); //休眠1s

System.out.println(“Thread_”+Thread.currentThread().getId()

+" ready init work step 1st…");

latch.countDown();//每完成一步初始化工作,扣减一次

System.out.println(“begin step 2nd…”);

SleepTools.ms(1);

System.out.println(“Thread_”+Thread.currentThread().getId()

+" ready init work step 2nd…");

latch.countDown();//每完成一步初始化工作,扣减一次

}

}).start();

new Thread(new BusiThread()).start();

for(int i=0;i<=3;i++){

Thread thread = new Thread(new InitThread());

thread.start();

}

latch.await();

System.out.println(“Main do ites work…”);

}

}

2、CyclicBarrier

让一组线程达到某个屏障,被阻塞,一直到组内最后一个线程达到屏障时,屏障开放,所有被阻塞的线程会继续运行CyclicBarrier(int parties)

CyclicBarrier(int parties, Runnable barrierAction),屏障开放,barrierAction定义的任务会执行

代码示例:

import java.util.Map;

import java.util.Random;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.CyclicBarrier;

/**

  • @Auther: BlackKingW

  • @Date: 2019/4/14 12:09

  • @Description:

*/

public class UseCyclicBarrier {

private static CyclicBarrier barrier

= new CyclicBarrier(5,new CollectThread());

private static ConcurrentHashMap<String,Long> resultMap

= new ConcurrentHashMap<>();//存放子线程工作结果的容器

public static void main(String[] args) {

for(int i=0;i<=4;i++){

Thread thread = new Thread(new SubThread());

thread.start();

}

}

//负责屏障开放以后的工作

private static class CollectThread implements Runnable{

@Override

public void run() {

StringBuilder result = new StringBuilder();

for(Map.Entry<String,Long> workResult:resultMap.entrySet()){

result.append(“[”+workResult.getValue()+“]”);

}

System.out.println(" the result = "+ result);

System.out.println(“do other business…”);

}

}

//工作线程

private static class SubThread implements Runnable{

@Override

public void run() {

long id = Thread.currentThread().getId();//线程本身的处理结果

resultMap.put(Thread.currentThread().getId()+“”,id);

Random r = new Random();//随机决定工作线程的是否睡眠

try {

if(r.nextBoolean()) {

Thread.sleep(2000+id);

System.out.println(“Thread_”+id+" …do something ");

}

System.out.println(id+“…is await”);

barrier.await();

Thread.sleep(1000+id);

System.out.println(“Thread_”+id+" …do its business ");

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

CountDownLatch和CyclicBarrier辨析

1、countdownlatch放行由第三者控制,CyclicBarrier放行由一组线程本身控制

2、countdownlatch放行条件》=线程数,CyclicBarrier放行条件=线程数

3、Semaphore

控制同时访问某个特定资源的线程数量,用在流量控制

import java.sql.Connection;

import java.util.LinkedList;

import java.util.concurrent.Semaphore;

/**

  • @Auther: BlackKingW

  • @Date: 2019/4/14 12:09

  • @Description:

*/

public class DBPoolSemaphore {

private final static int POOL_SIZE = 10;

private final Semaphore useful,useless;//useful表示可用的数据库连接,useless表示已用的数据库连接

public DBPoolSemaphore() {

this. useful = new Semaphore(POOL_SIZE);

this.useless = new Semaphore(0);

}

//存放数据库连接的容器

private static LinkedList pool = new LinkedList();

//初始化池

总结

虽然面试套路众多,但对于技术面试来说,主要还是考察一个人的技术能力和沟通能力。不同类型的面试官根据自身的理解问的问题也不尽相同,没有规律可循。

上面提到的关于这些JAVA基础、三大框架、项目经验、并发编程、JVM及调优、网络、设计模式、spring+mybatis源码解读、Mysql调优、分布式监控、消息队列、分布式存储等等面试题笔记及资料

有些面试官喜欢问自己擅长的问题,比如在实际编程中遇到的或者他自己一直在琢磨的这方面的问题,还有些面试官,尤其是大厂的比如 BAT 的面试官喜欢问面试者认为自己擅长的,然后通过提问的方式深挖细节,刨根到底。
seful = new Semaphore(POOL_SIZE);

this.useless = new Semaphore(0);

}

//存放数据库连接的容器

private static LinkedList pool = new LinkedList();

//初始化池

总结

虽然面试套路众多,但对于技术面试来说,主要还是考察一个人的技术能力和沟通能力。不同类型的面试官根据自身的理解问的问题也不尽相同,没有规律可循。

[外链图片转存中…(img-5UsyNWeN-1720104690123)]

[外链图片转存中…(img-9TivQ2gh-1720104690124)]

上面提到的关于这些JAVA基础、三大框架、项目经验、并发编程、JVM及调优、网络、设计模式、spring+mybatis源码解读、Mysql调优、分布式监控、消息队列、分布式存储等等面试题笔记及资料

有些面试官喜欢问自己擅长的问题,比如在实际编程中遇到的或者他自己一直在琢磨的这方面的问题,还有些面试官,尤其是大厂的比如 BAT 的面试官喜欢问面试者认为自己擅长的,然后通过提问的方式深挖细节,刨根到底。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值