博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 中的并发工具类
阅读量:5274 次
发布时间:2019-06-14

本文共 7561 字,大约阅读时间需要 25 分钟。

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
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
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
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。

转载于:https://www.cnblogs.com/hglibin/p/10853822.html

你可能感兴趣的文章
linux、sql 常用的一些特殊符号
查看>>
无限级分类实现思路
查看>>
函数mod(a,m)
查看>>
学习MySQL我们应该知道哪些东西?
查看>>
java工程中的.classpathaaaaaaaaaaaaaaaa<转载>
查看>>
cordova打包app后发请求出现 Provisional headers are shown的问题
查看>>
PHP-SESSION深入理解
查看>>
.net面试题目51-100
查看>>
[转]Android 代码自动提示功能
查看>>
算法题:找出一个数组中相加值最大的连续序列元素
查看>>
Java VisualVM插件地址
查看>>
[QPlugins]学习大纲
查看>>
hdu 3335
查看>>
触摸开关电路
查看>>
Mysql-ROW_FORMAT
查看>>
QButtonGroup
查看>>
像这样的作业调度器,你会怎么设计?
查看>>
MySQL基本语法
查看>>
显式锁select for update 用法
查看>>
程序员应该具备的能力
查看>>