Java分布式學習資料:lesson-9- Java concurrent data collection_第1頁
Java分布式學習資料:lesson-9- Java concurrent data collection_第2頁
Java分布式學習資料:lesson-9- Java concurrent data collection_第3頁
Java分布式學習資料:lesson-9- Java concurrent data collection_第4頁
Java分布式學習資料:lesson-9- Java concurrent data collection_第5頁
已閱讀5頁,還剩13頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Java Concurrent Data Collections圖示單擊此處添加段落文字內容1dsds此處添加段落文字內容2單擊此處添加段落文字內容3單擊此處添加段落文字內容4ConcurrentHashMap HashMap操作創建對象、 增加元素、刪除元素、遍歷元素、判斷元素是否包含在Vector中、判斷對象在元素中的位置等操作。 Put(Object key, Object Value); Remove(Object key); Contains(Object key) get(Object key); ConcurrentHashMap (Fragment) ConcurrentHas

2、hMap hashmapTest = new ConcurrentHashMap(); for(int step = 0; step 10; step+) String temp = null; temp = String.format(%d, step); hashmapTest.put(temp, step*10.0); Scanner scan = new Scanner(System.in); int index = scan.nextInt();HashMap (Fragment) String tmpkey = String.format(%d, index); if(hashma

3、pTest.containsKey(tmpkey) System.out.println(found the key); else System.out.println(Not found the key); Iterator ite; ite = hashmapTest.keySet().iterator(); while(ite.hasNext() HashMap (Fragment) String tempkey = (String)ite.next(); System.out.println(Key + tempkey + value : + hashmapTest.get(tempk

4、ey); ite = hashmapTest.entrySet().iterator(); while(ite.hasNext() Map.Entry entry = (Map.Entry) ite.next(); String key = entry.getKey(); Double value = entry.getValue(); System.out.println(Key + key + value : + value); CopyOnWriteArrayList CopyOnWriteArrayList操作創建對象、 增加元素、刪除元素、遍歷元素、判斷元素是否包含在Vector中、

5、判斷對象在元素中的位置等操作。在讀操作無鎖的ArrayList。 add(Object value); get(Object key); Remove(Object key); Contains(Object key) CopyOnWriteArraySet CopyOnWriteArraySet基于CopyOnWriteArrayList實現,與CopyOnWriteArrayList不同的是CopyOnWriteArraySet的add調用的是addIfAbsent方法, addIfAbsent受鎖保護。 ArrayBlockingQueue ArrayBlockingQueue是基于數組

6、、先進先出、線程安全的集合類,可實現指定時間的阻塞讀寫。 加入元素方法,add, offer, put等,其中offer可在數據已滿時進行等待,直至被喚醒、到達指定時間或線程被中斷。 LinkedBlockingQueue ArrayBlockingQueue是基于數組、先進先出、線程安全的集合類,可實現指定時間的阻塞讀寫。 加入元素方法,add, offer, put等,其中offer可在數據已滿時進行等待,直至被喚醒、到達指定時間或線程被中斷。 LinkedBlockingQueue是基于鏈表的線程隊列,在隊尾寫、隊首讀,采用兩把鎖。 AtomicInteger AtomicInteger

7、是支持原子操作的Integer類,類似的類有AtomicLong,AtomicBoolean等。Atomic類在每次獲取時自動加鎖。 private static AtomicInteger cunter = new AtomicInteger (); public static int getNetxtId() return counter.incrementAndGet(); ThreadPoolExecutor ThreadPoolExecutor提供線程池服務,可把實現了Runnable接口的任務放入線程池中執行。 ThreadPoolExecutor ThreadPoolExecut

8、or提供線程池服務,可把實現了Runnable接口的任務放入線程池中執行。 Executors提供了一些方法用于創建ThreadPoolExecutor,例如,newSingleThreadExecutor, newCachedThreadPool, newScheduledThreadPool等。 FutureTask FutureTask用于以異步方式獲取結果或取消執行任務。把Runnable或Callable的任務傳給FutureTask,直接調用run方法或放入線程池執行,之后通過FutureTask的get獲取執行結果。 Semaphore, CountDownLatch Semap

9、hore是用于控制資源同時被訪問的個數的類。構造函數Semaphore(int); tryAcquire(long, TimeUnit);Release(); CountDownLatch是一個控制多線程同時開始某動作的類,采用減計數的方式,當計數器為零時,位于latch.await后的代碼才會執行。 CountDownLatch(int); await(); countdown(); CyclicBarrierCyclicBarrier和CountDownLatch不同,是當await的數量到達了設定的數量后,才繼續執行。 常用函數CyclicBarrier(int); await(); ReentrantLock, ConditionReentrantLock提供了更為方便的方式控制并發資源。主要函數是ReentrantLock(); lock(); unlock(); Condit

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論