日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java的concurrent包

發布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的concurrent包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

最近在網上看到一個將concurrent包的系列文章,仔細讀了一遍,感覺不錯。

分享一下:http://www.hetaoblog.com/myblogs/post/%E8%AF%B4%E4%B8%80%E8%AF%B4java%E7%9A%84concurrent%E5%8C%851-%E6%95%B4%E4%BD%93%E4%BB%8B%E7%BB%8D.jhtml

Concurrent包主要有三個package組成。java.util.concurrent:提供大部分關于并發的接口和類,如BlockingQueue,Callable,ConcurrentHashMap,ExecutorService, Semaphore等。

java.util.concurrent.atomic:提供所有原子操作的類, 如AtomicInteger, AtomicLong等;?
java.util.concurrent.locks:提供鎖相關的類, 如Lock, ReentrantLock, ReadWriteLock, Condition等;

?CountDownLatch

?CountDownLatch等待多個線程完成執行,這個類會等待多個子線程結束后才開始執行主線程的其他操作。直接上一個例子。

@Testpublic void testCountDown(){int count = 10;final CountDownLatch l = new CountDownLatch(count);for (int i = 0; i < count; ++i){final int index = i;new Thread(new Runnable() {@Overridepublic void run() {try {Thread.currentThread().sleep(3 * 1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("thread " + index + " has finished...");l.countDown();}}).start();}try {l.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("now all threads have finished");} 這個例子中,結束語句會在所有線程結束后再執行,否則會一直await。


Atomic類

Atomic相關類是線程安全的,支持無阻塞無鎖定的操作set(),get(),getAndSet(), getAndIncrement(),getAndDecrement(),getAndAdd()


@Testpublic void testAtomic(){final int loopcount = 10000;int threadcount = 10;final NonSafeSeq seq1 = new NonSafeSeq();final SafeSeq seq2 = new SafeSeq();final CountDownLatch l = new CountDownLatch(threadcount);for(int i = 0; i < threadcount; ++i){final int index = i;new Thread(new Runnable() {@Overridepublic void run() {for(int j = 0; j < loopcount; ++j){seq1.inc();seq2.inc();}System.out.println("finished : " + index);l.countDown();}}).start();}try {l.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("both have finished....");System.out.println("NonSafeSeq:" + seq1.get());System.out.println("SafeSeq with atomic: " + seq2.get());} class NonSafeSeq{private long count = 0;public void inc(){count++;}public long get(){return count;} }class SafeSeq{private AtomicLong count = new AtomicLong(0);public void inc(){count.incrementAndGet();}public long get(){return count.longValue();} } 輸出結果如下:



both have finished.... NonSafeSeq:93212 SafeSeq with atomic: 100000 很明顯看出來在原始方法中即使做了10000次相加操作也沒有辦法保證多線程下的一致性。使用AtomicLong進行相加可以避免這樣的問題。


ReentrantLock類

之前一直是使用synchronized關鍵字來保證線程間的同步。concurrent包提供了ReentrantLock類,該類提供lock()和unlock()方法

1. 是更好的性能,?
2. 提供同一個lock對象上不同condition的信號通知?
3. 還提供lockInterruptibly這樣支持響應中斷的加鎖過程,意思是說你試圖去加鎖,但是當前鎖被其他線程hold住,然后你這個線程可以被中斷;

這里直接上下相關lock操作的代碼

class SafeSeqWithLock{private long count = 0;private ReentrantLock lock = new ReentrantLock();public void inc(){lock.lock();try{count++;}finally{lock.unlock();}}public long get() {return count;}} 可以看到,這里在++操作之前先lock了,然后再執行了unlock操作。


ReadWriteLock

讀鎖可以有很多個鎖同時上鎖,只要當前沒有寫鎖;?
寫鎖是排他的,上了寫鎖,其他線程既不能上讀鎖,也不能上寫鎖;同樣,需要上寫鎖的前提是既沒有讀鎖,也沒有寫鎖;

試圖獲得寫鎖的線程只有當另外一個線程將讀鎖釋放了以后才可以獲得

@Testpublic void testRWLock_getw_onr(){ReentrantReadWriteLock lock = new ReentrantReadWriteLock();final Lock rlock = lock.readLock();final Lock wlock = lock.writeLock();final CountDownLatch l = new CountDownLatch(2);// start r threadnew Thread(new Runnable() {@Overridepublic void run() {System.out.println(new Date() + "now to get rlock");rlock.lock();try {Thread.currentThread().sleep(3 * 1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(new Date() + "now to unlock rlock");rlock.unlock();l.countDown();}}).start();// start w threadnew Thread(new Runnable() {@Overridepublic void run() {System.out.println(new Date() + "now to get wlock");wlock.lock();System.out.println(new Date() + "now to unlock wlock");wlock.unlock();l.countDown();}}).start();try {l.await();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(new Date() + "finished");}


總結一下。這里是concurrent包中常用的幾個類。集中涉及了多線程,并發控制,鎖的相關機制。


轉載于:https://my.oschina.net/zimingforever/blog/143914

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的java的concurrent包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。