【Java】Java同步组件之Condition,FutureTask

Java同步组件概况

  • CountDownLatch : 是闭锁,通过一个计数来保证线程是否一直阻塞
  • Semaphore: 控制同一时间,并发线程数量
  • CyclicBarrier:字面意思是回环栅栏,通过它可以实现让一组线程等待至某个状态之后再全部同时执行。
  • ReentrantLock:是一个重入锁,一个线程获得了锁之后仍然可以反复加锁,不会出现自己阻塞自己的情况。
  • Condition:配合ReentrantLock,实现等待/通知模型

  • FutureTask:FutureTask实现了接口Future,同Future一样,代表异步计算的结果。

Condition

Condition代码演示

package com.rumenz.task;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;

public class ConditionExample {

public static void main(String[] args) {

ReentrantLock reentrantLock=new ReentrantLock();

Condition condition=reentrantLock.newCondition();

ExecutorService executorService = Executors.newCachedThreadPool();

AtomicInteger atomicInteger=new AtomicInteger(0);

executorService.execute(()->{

try{

reentrantLock.lock();

System.out.println("计算1====开始");

int i = atomicInteger.addAndGet(10);

System.out.println("计算1====结果"+i);

condition.await();

atomicInteger.incrementAndGet();

System.out.println("最后结果"+atomicInteger.get());

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

reentrantLock.unlock();

}

});

executorService.execute(()->{

try{

reentrantLock.lock();

Thread.sleep(5000);

System.out.println("计算====2");

int i = atomicInteger.addAndGet(40);

System.out.println("计算2====结果"+i);

condition.signal();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

reentrantLock.unlock();

}

});

executorService.shutdown();

}

}

//计算1====开始

//计算1====结果10

//计算====2

//计算2====结果50

//最后结果51

FutureTask

FutureTask代码演示

package com.rumenz.task;

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.FutureTask;

public class FutureTaskExample {

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

List<FutureTask<Integer>> futureTasks=new ArrayList<>();

ExecutorService pool = Executors.newFixedThreadPool(5);

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

FutureTask<Integer> futureTask=new FutureTask<>(()->{

int res=0;

for (int j = 0; j < 50; j++) {

res+=10;

}

return res;

});

futureTasks.add(futureTask);

pool.submit(futureTask);

}

System.out.println("所有任务都已经提交,主线程开始干活");

Thread.sleep(5000);//模拟主线程的任务

System.out.println("主进程任务完成,开始获取子线程任务结果");

int res=0;

for(FutureTask<Integer> task:futureTasks){

try{

res+=task.get();

}catch (Exception e){

e.printStackTrace();

}

}

pool.shutdown();

System.out.println("最终计算结果:"+res);

}

}

//所有任务都已经提交,主线程开始干活

//主进程任务完成,开始获取子线程任务结果

//最终计算结果:5000

关注微信公众号:【入门小站】,解锁更多知识点

【Java】Java同步组件之Condition,FutureTask

以上是 【Java】Java同步组件之Condition,FutureTask 的全部内容, 来源链接: utcz.com/a/102594.html

回到顶部