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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ 异步下获取线程执行结果_这份阿里技术官强推的java线程池笔记,建议你看一下

發(fā)布時間:2023/12/13 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ 异步下获取线程执行结果_这份阿里技术官强推的java线程池笔记,建议你看一下 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

線程池

  • 線程是寶貴的內(nèi)存資源,單個線程占1MB空間,過多分配易造成內(nèi)存溢出
  • 頻繁的創(chuàng)建及銷毀線程會增加虛擬機回收頻率、資源開銷、造成程序性能下降
  • 因此線程池出現(xiàn)了

線程池的概念

  • 線程容器,可設(shè)定線程分配的數(shù)量上限
  • 將預(yù)先創(chuàng)建的線程對象存入池中,并重用線程池中的線程對象
  • 避免頻繁的創(chuàng)建和銷毀

線程池的原理

獲取線程池

創(chuàng)建線程池

public class TestThreadPool { public static void main(String[] args) { // 1. 創(chuàng)建固定線程個數(shù)的線程池對象 //線程池里可存在4個線程 ExecutorService es = Executors.newFixedThreadPool(4); // 2. 創(chuàng)建任務(wù) Runnable runnable = new Runnable() { private int ticket = 100; @Override public void run() { while (true) { if(ticket <= 0) { break; } System.out.println(Thread.currentThread().getName() + "買了" + ticket--); } } }; // 3. 提交任務(wù) for(int i = 0; i < 5; i++) { es.submit(runnable); } // 4. 關(guān)閉線程池 es.shutdown(); }}

執(zhí)行結(jié)果

// 1. 創(chuàng)建單線程的線程池 ExecutorService es = Executors.newSingleThreadExecutor();// 1. 創(chuàng)建緩沖線程池,線程的個數(shù)由任務(wù)來決定 ExecutorService es = Executors.newCachedThreadPool();

Callable接口

public interface Callable{ public V call() throws Exception;}
  • JDK5加入,與Runnable接口類似,實現(xiàn)之后代表一個線程任務(wù)
  • Callable具有泛型返回值、可以聲明異常

Future接口

  • 概念:異步接受ExecutorService.submit()所返回的狀態(tài)結(jié)果,當(dāng)中包含了call()的返回值
  • 方法:V get()以阻塞形式等待Future中的異步處理結(jié)果(call()的返回值)

示例

  • 使用Future和Callable接口
  • 使用兩個線程,并發(fā)計算1-50,51-100的和,再進行匯總
//示例public class TestCallable { public static void main(String[] args) throws Exception { // 1.創(chuàng)建線程池對象 ExecutorService es = Executors.newFixedThreadPool(2); // 2. 提交任務(wù)并得到Future對象,任務(wù)有Callable匿名對象來承擔(dān) Future future1 = es.submit(new Callable() { @Override public Integer call() throws Exception { //完成1-50的加和運算并得到結(jié)果 System.out.println("start 1-50 count..."); int sum = 0; for(int i = 1; i <= 50; i++) { sum += i; } return sum; } }); Future future2 = es.submit(new Callable() { @Override public Integer call() throws Exception { //完成51-100的加和運算并得到結(jié)果 System.out.println("start 51 - 100 count..."); int sum = 0; for(int i = 51; i <= 100; i++) { sum += i; } return sum; } }); // 3. 使用Future對象的get()方法得到運算結(jié)果 System.out.println("1-100的加和結(jié)果為:" + (future1.get() + future2.get())); // 4. 關(guān)閉資源 es.shutdown(); }}

結(jié)果

Lock接口

  • JDK5加入,與synchronized比較,顯示定義,結(jié)構(gòu)更靈活
  • 提供更多實用性方法,功能更加強大,性能更優(yōu)越

常用方法

//獲取鎖,如鎖被占用,則等待void lock() //嘗試獲取鎖(成功true,失敗false,不阻塞)boolean tryLock//釋放鎖void unlock()//示例public class TestLock implements Runnable { Lock l = new ReentrantLock(); private int ticket = 100; @Override public void run() { while (true) { l.lock(); try { if(ticket <= 0) { break; } System.out.println(Thread.currentThread().getName() + " sells " + ticket--); } catch (Exception e) { e.printStackTrace(); }finally { l.unlock(); } } } public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(4); for(int i = 0; i < 4; i++) { es.submit(new TestLock()); } es.shutdown(); }}

執(zhí)行結(jié)果

線程安全的集合

Collections中的工具方法

Queue接口(隊列)

//示例public class TestQueue { public static void main(String[] args) { // 1. 創(chuàng)建PriorityQueue隊列對象 PriorityQueue q = new PriorityQueue(); // 2. 隊列添加元素,“入列” q.offer("a"); q.offer("b"); q.offer("c"); q.offer("d"); q.offer("e"); // 3. 取值,“出列” System.out.println(q.poll()); System.out.println(q.poll()); System.out.println(q.poll()); System.out.println(q.poll()); System.out.println(q.poll()); }}

執(zhí)行結(jié)果

最后

感謝你看到這里,看完有什么的不懂的可以在評論區(qū)問我,覺得文章對你有幫助的話記得給我點個贊,每天都會分享java相關(guān)技術(shù)文章或行業(yè)資訊,歡迎大家關(guān)注和轉(zhuǎn)發(fā)文章!

總結(jié)

以上是生活随笔為你收集整理的c++ 异步下获取线程执行结果_这份阿里技术官强推的java线程池笔记,建议你看一下的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。