Java笔记-对CountDownLatch的理解(对比Qt中的QSemaphore)含实例
首先在CountDownLatch,這個(gè)東西基本上和信號(hào)量是一樣的,這個(gè)CountDownLatch要設(shè)置一個(gè)初值,這個(gè)值一般是個(gè)正值,可以對(duì)這個(gè)CountDownLatch進(jìn)行countDown()也就是減1,當(dāng)減少為0時(shí),await()函數(shù)就會(huì)從阻塞變成就緒態(tài)。
?
下面是Java代碼,舉個(gè)例子,此例子來(lái)源于博客園大神,3個(gè)工人干活,3個(gè)都干完后,老版才開(kāi)始檢查工作,運(yùn)行截圖如下:
源碼如下:
?Boss.java
package cn.it1995;import java.util.concurrent.CountDownLatch;public class Boss implements Runnable{private CountDownLatch downLatch;public Boss(CountDownLatch downLatch){this.downLatch = downLatch;}public void run() {System.out.println("老版在等待所有工人干完活....");try {this.downLatch.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("工人都干完活了,老版開(kāi)始檢查");} }Worker.java
package cn.it1995;import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class Main {public static void main(String[] args){ExecutorService executorService = Executors.newCachedThreadPool();CountDownLatch latch = new CountDownLatch(3);Worker w1 = new Worker(latch, "張三");Worker w2 = new Worker(latch, "李四");Worker w3 = new Worker(latch, "王二");Boss boss = new Boss(latch);executorService.execute(w1);executorService.execute(w2);executorService.execute(w3);executorService.execute(boss);executorService.shutdown();} }Main.java
package cn.it1995;import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit;public class Worker implements Runnable{private CountDownLatch downLatch;private String name;public Worker(CountDownLatch downLatch, String name){this.downLatch = downLatch;this.name = name;}private void doWork(){System.out.println(this.name + " 正在干活...");}public void run() {this.doWork();try{TimeUnit.SECONDS.sleep(new Random().nextInt(10));}catch(InterruptedException e){e.printStackTrace();}this.downLatch.countDown();System.out.println(this.name + " 干完活了!");} }源碼打包下載地址:
https://github.com/fengfanchen/Java/tree/master/CountDownLatchDemo
?
這里我們來(lái)對(duì)比下Qt里面的信號(hào)量QSemaphore,其實(shí)上面那個(gè)CountDownLatch基本上等同于信號(hào)量,只是基本上啊,不過(guò)信號(hào)量可以干很多東西,不限于上面的。
程序運(yùn)行截圖如下:
源碼如下:
Boss.h
#ifndef BOSS_H #define BOSS_H#include <QObject>QT_BEGIN_NAMESPACE class QSemaphore; QT_END_NAMESPACEclass Boss : public QObject {Q_OBJECT public:Boss(QSemaphore *sem, QObject *parent = nullptr);~Boss();public slots:void doWork();private:QSemaphore *m_sem;};#endif // BOSS_HWorker.h
#ifndef WORKER_H #define WORKER_H#include <QObject>QT_BEGIN_NAMESPACE class QSemaphore; QT_END_NAMESPACEclass Worker : public QObject {Q_OBJECT public:Worker(QSemaphore *sem, QString name, QObject *parent = nullptr);~Worker();public slots:void doWork();private:QString m_name;QSemaphore *m_sem; };#endif // WORKER_HBoss.cpp
#include "Boss.h" #include <QThread> #include <QDebug> #include <QSemaphore>Boss::Boss(QSemaphore *sem, QObject *parent) : QObject(parent) {m_sem = sem; }Boss::~Boss() {qDebug() << "BOSS 析構(gòu)"; }void Boss::doWork() {qDebug() << "子線程[" << QThread::currentThread() << "]: " << "BOSS" << " 等待所有人完成工作...";m_sem->acquire(1);qDebug() << "子線程[" << QThread::currentThread() << "]: " << "BOSS" << " 開(kāi)始檢測(cè)工作!!!"; }main.cpp
#include <QCoreApplication> #include <QSemaphore> #include <QThread> #include <QDebug> #include "Worker.h" #include "Boss.h"int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QSemaphore sem(-2);qDebug() <<"主線程[" << QThread::currentThread() << "]: 開(kāi)始運(yùn)行";QThread thread1;QThread thread2;QThread thread3;QThread thread4;Worker worker1(&sem, "張三");Worker worker2(&sem, "李四");Worker worker3(&sem, "王二");Boss boss(&sem);worker1.moveToThread(&thread1);worker2.moveToThread(&thread2);worker3.moveToThread(&thread3);boss.moveToThread(&thread4);QObject::connect(&thread1, &QThread::started, &worker1, &Worker::doWork);QObject::connect(&thread1, &QThread::finished, &worker1, &QObject::deleteLater);QObject::connect(&thread2, &QThread::started, &worker2, &Worker::doWork);QObject::connect(&thread2, &QThread::finished, &worker2, &QObject::deleteLater);QObject::connect(&thread3, &QThread::started, &worker3, &Worker::doWork);QObject::connect(&thread3, &QThread::finished, &worker3, &QObject::deleteLater);QObject::connect(&thread4, &QThread::started, &boss, &Boss::doWork);QObject::connect(&thread4, &QThread::finished, &boss, &QObject::deleteLater);thread1.start();QThread::sleep(1); //為了產(chǎn)生不同的隨機(jī)種子thread2.start();QThread::sleep(1); //為了產(chǎn)生不同的隨機(jī)種子thread3.start();thread4.start();return a.exec(); }Worker.cpp
#include "Worker.h" #include <QSemaphore> #include <QThread> #include <QTime> #include <QDebug>Worker::Worker(QSemaphore *sem, QString name, QObject *parent) : QObject(parent) {m_sem = sem;m_name = name; }Worker::~Worker() {qDebug() << m_name << " 析構(gòu)"; }void Worker::doWork() {qsrand(QTime::currentTime().msec() + QTime::currentTime().second()*1000);qDebug() << "子線程[" << QThread::currentThread() << "]: " << m_name << " 正在干活...";QThread::sleep(qrand() % 10 + 5);qDebug() << "子線程[" << QThread::currentThread() << "]: " << m_name << " 活干完了!!!";m_sem->release(1); }源碼打包下載地址:
https://github.com/fengfanchen/Qt/tree/master/SemaphoreTest
總結(jié)
以上是生活随笔為你收集整理的Java笔记-对CountDownLatch的理解(对比Qt中的QSemaphore)含实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python笔记-使用cython生成d
- 下一篇: Linux笔记-bash中字符串拆分并且