Java 中的并发工具类

CountDownLatch

public class JoinCountDownLatchTest {
public static void main(String[] args) throws InterruptedException {
Thread parser1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("parser1 finish!");
}
}); Thread parser2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("parser2 finish!");
}
}); parser1.start();
parser2.start();
parser1.join();
parser2.join();
System.out.println("all parser finish!");
}
}

运行结果

parser1 finish!

parser2 finish!

all parser finish!



parser2 finish!

parser1 finish!

all parser finish!

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {
private static CountDownLatch count = new CountDownLatch(2); public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(1);
count.countDown();
System.out.println(2);
count.countDown();
}
}); t1.start();
try {
count.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(3);
}
}

运行结果

1

2

3

CyclicBarrier

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; public class CyclicBarrierTest {
private static CyclicBarrier c = new CyclicBarrier(2); public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(1);
}
}); t.start(); try {
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(2);
}
}

运行结果

1

2



2

1

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; public class CyclicBarrierTest2 {
private static CyclicBarrier c = new CyclicBarrier(2, new A()); public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(1);
}
}); t.start();
try {
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(2);
} private static class A implements Runnable {
@Override
public void run() {
System.out.println(3);
}
}
}

运行结果

3

1

2



3

2

1

import java.util.Map;
import java.util.concurrent.*; public class BankWaterService implements Runnable {
private CyclicBarrier c = new CyclicBarrier(4, this);
private ExecutorService executor = Executors.newFixedThreadPool(4);
private ConcurrentHashMap<String, Integer> sheetBankWaterCount = new ConcurrentHashMap<>(); private void count() {
for (int i = 0; i < 4; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
sheetBankWaterCount.put(Thread.currentThread().getName(), 1);
try {
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
executor.shutdown();
} @Override
public void run() {
int result = 0;
for (Map.Entry<String, Integer> sheet : sheetBankWaterCount.entrySet()) {
result += sheet.getValue();
}
sheetBankWaterCount.put("result", result);
System.out.println(result);
} public static void main(String[] args) {
BankWaterService bankWaterCount = new BankWaterService();
bankWaterCount.count();
}
}

运行结果

4

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest3 {
private static CyclicBarrier c = new CyclicBarrier(2); public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
c.await();
} catch (Exception e) {
}
}
});
t.start(); Thread.sleep(1000);
System.out.println(c.getNumberWaiting());
t.interrupt();
try {
c.await();
} catch (Exception e) {
System.out.println(c.isBroken());
}
}
}

运行结果

1

true

Semaphore

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore; public class SemaphoreTest {
private static final int THREAD_COUNT = 30;
private static ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
private static Semaphore s = new Semaphore(10); public static void main(String[] args) {
for (int i = 0; i < THREAD_COUNT; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
s.acquire();
System.out.println("save data!");
s.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
executor.shutdown();
}
}

Exchanger

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ExchangerTest {
private static final Exchanger<String> exgr = new Exchanger<>();
private static ExecutorService executor = Executors.newFixedThreadPool(2); public static void main(String[] args) {
executor.execute(new Runnable() {
@Override
public void run() {
String A = "银行流水 A";
try {
exgr.exchange(A);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}); executor.execute(new Runnable() {
@Override
public void run() {
String B = "银行流水 B";
try {
String A = exgr.exchange(B);
System.out.println("A 和 B 数据是否一致:" + A.equals(B) + ",\nA 录入的是:" + A + ",\nB 录入的是:" + B + "。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}); executor.shutdown();
}
}

运行结果

A 和 B 数据是否一致:false,

A 录入的是:银行流水 A,

B 录入的是:银行流水 B。

Java 中的并发工具类的更多相关文章

  1. Java中的并发工具类(CountDownLatch、CyclicBarrier、Semaphore、Exchanger)

    在JDK的并发包里提供了很多有意思的并发工具类.CountDownLatch.CyclicBarrier和Semaphore 工具类提供了一种并发流程控制的手段,Exchanger 工具类则提供了在线 ...

  2. java中的并发工具类

    在jdk的并发包里提供了几个非常有用的并发工具类.CountDownLatdch.CyclicBarrier和Semaphore工具类提供了一种并发流程控制的手段,Exchanger工具类则提供了在线 ...

  3. 第八章 Java中的并发工具类

    等待多线程完成的CountDownLatch countDownLatch允许一个或多个线程等待其他线程完成操作. public class CountDownLatchTest { static C ...

  4. Java中的并发工具类:CountDownLatch、CyclicBarrier和Semaphore

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法. 一. ...

  5. 第8章 java中的并发工具类

    8.1 等待线程完成的CountDownLatch 作用:让一个线程等待其余线程完成之后在继续执行,如主线程等待开启服务的子线程执行完毕后主线程继续执行,类似于join.

  6. Java线程的并发工具类

    Java线程的并发工具类. 一.fork/join 1. Fork-Join原理 在必要的情况下,将一个大任务,拆分(fork)成若干个小任务,然后再将一个个小任务的结果进行汇总(join). 适用场 ...

  7. JAVA中封装JSONUtils工具类及使用

    在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...

  8. java中常用的工具类(一)

    我们java程序员在开发项目的是常常会用到一些工具类.今天我汇总了一下java中常用的工具方法.大家可以在项目中使用.可以收藏!加入IT江湖官方群:383126909 我们一起成长 一.String工 ...

  9. 并发06--JAVA中的并发工具类

    1.等待多线程完成的CountDownLatch CountDownLatch允许一个或多个线程等待其他线程完成操作. 使用join也可以完成这个操作,代码示例如下: package com.exam ...

随机推荐

  1. Deepin Create/Delete Folder refresh

    Did u have a problem whth the deepin file manager,Everthime I create/delete a Folder of File i have ...

  2. 坑:jmeter代理服务器录制脚本出现target controller is configured to "use recording Controller" but no such controller exists...

    配置好代理服务器后,运行代理服务器 run 报错: target controller is configured to "use recording Controller" bu ...

  3. 【CSP模拟赛】独立集(最长上升子序列&大力猜结论)

    题目描述 有一天,一个名叫顺旺基的程序员从石头里诞生了.又有一天,他学会了冒泡排序和独 立集.在一个图里,独立集就是一个点集,满足任意两个点之间没有边.于是他就想把这两 个东西结合在一起.众所周知,独 ...

  4. 算法的时间复杂度O

    一.时间复杂度 在进行算法分析时,语句总的执行次数 T(n) 是关于问题的规模n 的函数,进而分析 T(n) 随 n 的变化情况并确定 T(n) 的数量级,算法的时间复杂度,也就是算法的时间度量,记作 ...

  5. [转]EL表达式判断是否为空,判断是否为空字符串

    原文地址:https://blog.csdn.net/zhaofuqiangmycomm/article/details/79442730 El表达式判断是否为空字符串 ${empty 值}  返回t ...

  6. Python中产生随机数

    Python中产生随机数 一.Python自带的random库       1.参生n--m范围内的一个随机数:    random.randint(n,m) 2.产生0到1之间的浮点数:  rand ...

  7. 使用SoapUI发送Post请求

    https://www.cnblogs.com/xiaowangzi1110/p/8544264.html 使用SoapUI发送Post请求 SoapUI作为一个开源的工具,其具备强大的功能.易用的界 ...

  8. Oracle数据库查看表空间sql语句

    转: Oracle数据库查看表空间sql语句 2018-09-03 15:49:51 兰海泽 阅读数 6212   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...

  9. 泡泡一分钟:Perception-aware Receding Horizon Navigation for MAVs

    作为在空中抛掷四旋翼飞行器后恢复的第一步,它需要检测它使用其加速度计的发射.理想的情况下,在飞行中,加速度计理想地仅测量由于施加的转子推力引起的加速度,即.因此,当四旋翼飞行器发射时,我们可以检测到测 ...

  10. 将C++资源文件读取出来

    HRSRC   hResource   =   FindResource(GetModuleHandle(NULL),     MAKEINTRESOURCE(IDR_CALC),   TEXT(&q ...