• /  51
  • 下載費用: 9.90積分  

Java語言程序設計10E LiangPPT習題案例30slide.ppt

'Java語言程序設計10E LiangPPT習題案例30slide.ppt'
*Chapter 30 Multithreading and Parallel Programming*ObjectivesTo get an overview of multithreading (§30.2).To develop task classes by implementing the Runnable interface (§30.3).To create threads to run tasks using the Thread class (§30.3).To control threads using the methods in the Thread class (§30.4).To control animations using threads and use Platform.runLater to run the code in application thread (§30.5).To execute tasks in a thread pool (§30.6).To use synchronized methods or blocks to synchronize threads to avoid race conditions (§30.7).To synchronize threads using locks (§30.8).To facilitate thread communications using conditions on locks (§§30.9–30.10).To use blocking queues to synchronize access to an array queue, linked queue, and priority queue (§30.11).To restrict the number of accesses to a shared resource using semaphores (§30.12).To use the resource-ordering technique to avoid deadlocks (§30.13).To describe the life cycle of a thread (§30.14).To create synchronized collections using the static methods in the Collections class (§30.15).To develop parallel programs using the Fork/Join Framework (§30.16).*Threads ConceptMultiple threads on multiple CPUsMultiple threads sharing a single CPU*Creating Tasks and Threads*Example: Using the Runnable Interface to Create and Launch ThreadsObjective: Create and run three threads:The first thread prints the letter a 100 times. The second thread prints the letter b 100 times.The third thread prints the integers 1 through 100. TaskThreadDemoRun*The Thread Class *The Static yield() MethodYou can use the yield() method to temporarily release time for other threads. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows:public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); Thread.yield(); }} Every time a number is printed, the print100 thread is yielded. So, the numbers are printed after the characters. *The Static sleep(milliseconds) MethodThe sleep(long mills) method puts the thread to sleep for the specified time in milliseconds. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows:public void run() { for (int i = 1; i = 50) Thread.sleep(1); } catch (InterruptedException ex) { } }} Every time a number (>= 50) is printed, the print100 thread is put to sleep for 1 millisecond. *The join() MethodYou can use the join() method to force one thread to wait for another thread to finish. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows:The numbers after 50 are printed after thread printA is finished. *isAlive(), interrupt(), and isInterrupted()The isAlive() method is used to find out the state of a thread. It returns true if a thread is in the Ready, Blocked, or Running state; it returns false if a thread is new and has not started or if 。省略部分。 of five states: New, Ready, Running, Blocked, or Finished.*Synchronized Collections The classes in the Java Collections Framework are not thread-safe, i.e., the contents may be corrupted if they are accessed and updated concurrently by multiple threads. You can protect the data in a collection by locking the collection or using synchronized collections.The Collections class provides six static methods for wrapping a collection into a synchronized version. The collections created using these methods are called synchronization wrappers.*The Fork/Join FrameworkThe widespread use of multicore systems has created a revolution in software. In order to benefit from multiple processors, software needs to run in parallel. JDK 7 introduces the new Fork/Join Framework for parallel programming, which utilizes the multicore processors.*The Fork/Join FrameworkThe Fork/Join Framework is used for parallel programming in Java.In JDK 7’s Fork/Join Framework, a fork can be viewed as an independent task that runs on a thread.*ForkJoinTask and ForkJoinPoolThe framework defines a task using the ForkJoinTask class, and executes a task in an instance of ForkJoinPool.*ForkJoinTask*ExamplesParallelMaxRunParallelMergeSortRun*Vector, Stack, and Hashtable Invoking synchronizedCollection(Collection c) returns a new Collection object, in which all the methods that access and update the original collection c are synchronized. These methods are implemented using the synchronized keyword. For example, the add method is implemented like this:public boolean add(E o) { synchronized (this) { return c.add(o); }}The synchronized collections can be safely accessed and modified by multiple threads concurrently. The methods in java.util.Vector, java.util.Stack, and Hashtable are already synchronized. These are old classes introduced in JDK 1.0. In JDK 1.5, you should use java.util.ArrayList to replace Vector, java.util.LinkedList to replace Stack, and java.util.Map to replace Hashtable. If synchronization is needed, use a synchronization wrapper. Companion Website*Fail-Fast The synchronization wrapper classes are thread-safe, but the iterator is fail-fast. This means that if you are using an iterator to traverse a collection while the underlying collection is being modified by another thread, then the iterator will immediately fail by throwing java.util.ConcurrentModificationException, which is a subclass of RuntimeException. To avoid this error, you need to create a synchronized collection object and acquire a lock on the object when traversing it. For example, suppose you want to traverse a set, you have to write the code like this:Set hashSet = Collections.synchronizedSet(new HashSet());synchronized (hashSet) { // Must synchronize it Iterator iterator = hashSet.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }} Failure to do so may result in nondeterministic behavior, such as ConcurrentModificationException.Companion Website
關 鍵 詞:
liangppt 習題 10 10e 案例 30slide 程序設計 語言 30 java
 天天文庫所有資源均是用戶自行上傳分享,僅供網友學習交流,未經上傳用戶書面授權,請勿作他用。
關于本文
本文標題:Java語言程序設計10E LiangPPT習題案例30slide.ppt
鏈接地址: http://www.476824.live/p-51617560.html
關于我們 - 網站聲明 - 網站地圖 - 資源地圖 - 友情鏈接 - 網站客服點擊這里,給天天文庫發消息,QQ:1290478887 - 聯系我們

本站為“文檔C2C交易模式”,即用戶上傳的文檔直接賣給(下載)用戶,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有【成交的100%(原創)】。本站是網絡服務平臺方,若您的權利被侵害,侵權客服QQ:1290478887 歡迎舉報。

[email protected] 2017-2027 http://www.476824.live 網站版權所有

粵ICP備19057495號 

收起
展開
球探网即时蓝球比分 短线股票推荐免费 佳永配资配资门户 1980的时时彩平台 甘肃快3基本走势图和值 湖北十一选五走势一定牛 福建11选五预测任选5 云南11远5开奖结果 七位数基本走势图表图 黑龙江十一选五19期开奖结果 手机photoshop软件