- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
Java并行执行
展开查看详情
1 .Concurrency with Java Varadha Sundaram
2 .Programming Paradigms Single Process Multi Process Multi Core/Multi Thread
3 .Multi Core CPU
4 .Process Process A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process are allocated to it via the operating system, e.g. memory and CPU time.
5 .Java Process Objects • ADTs, aggregate components, JavaBeans, monitors, business objects, remote RMI objects, subsystems, ... • May be grouped according to structure, role, ... • Usable across multiple activities — focus on SAFETY Activities • Messages, call chains, threads, sessions, scenarios, scripts, workflows, use cases, transactions, data flows, mobile computations
6 .Thread T hreads are so called lightweight processes which have their own call stack but an access shared data. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data
7 .Thread T hreads are so called lightweight processes which have their own call stack but an access shared data. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data
8 .How much is the speed up If the sequential portion of a program accounts for 10% of the runtime, we can get no more than a 10× speed-up, regardless of how many processors are added. This puts an upper limit on the usefulness of adding more parallel execution units. "When a task cannot be partitioned because of sequential constraints, the application of more effort has no effect on the schedule. The bearing of a child takes nine months, no matter how many women are assigned
9 .How does Java help to Parallel Programming Has Threads since its inception Threads have their own stack. But uses heap of the JVM Has many constructs to control the concurrency Programmer knowledge is very critical
10 .JVM Process Heap
11 .Thread Class Constructors Thread( Runnable r) constructs so run() calls r.run () — Other versions allow names, ThreadGroup placement Principal methods start() activates run() then returns to caller isAlive () returns true if started but not stopped join() waits for termination (optional timeout) interrupt() breaks out of wait, sleep, or join isInterrupted () returns interruption state getPriority () returns current scheduling priority setPriority ( int priorityFromONEtoTEN ) sets it
12 .How to Implement a Thread Subclass Thread. The Thread class itself implements Runnable , though its run method does nothing. An application can subclass Thread, providing its own implementation of run
13 .Thread Subclass public class HelloThread extends Thread { public void run() { System.out.println ("Hello from a thread!"); } public static void main(String args []) { (new HelloThread ()).start(); } }
14 .Implement Runnable Interface public class HelloRunnable implements Runnable { public void run() { System.out.println ("Hello from a thread!"); } public static void main(String args []) { (new HelloRunnable ()).start(); } }
15 .Pausing while executing Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing
16 .Pausing while executing Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing
17 .Pausing while executing Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing
18 .Pausing while executing Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing
19 .Interrupt requirement A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
20 .Interrupt Status The interrupt mechanism is implemented using an internal flag known as the interrupt status . Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted , interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.
21 .Interrupt Status The interrupt mechanism is implemented using an internal flag known as the interrupt status . Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted , interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.
22 .Interrupt Status The interrupt mechanism is implemented using an internal flag known as the interrupt status . Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted , interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.
23 .Thread Interruption Example
24 .Synchronization Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors . The tool needed to prevent these errors is synchronization .
25 .Synchronization Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors . The tool needed to prevent these errors is synchronization .
26 .Synchronization Libraries Semaphores Maintain count of the number of threads allowed to pass Latches Boolean conditions that are set once, ever Barriers Counters that cause all threads to wait until all have finished Reentrant Locks Java-style locks allowing multiple acquisition by same thread, but that may be acquired and released as needed Mutexes Non-reentrant locks Read/Write Locks Pairs of conditions in which the readLock may be shared, but the writeLock is exclusive
27 .Semaphores Conceptually serve as permit holders • Construct with an initial number of permits (usually 0) • require waits for a permit to be available, then takes one • release adds a permit But in normal implementations, no actual permits change hands. • The semaphore just maintains the current count. • Enables very efficient implementation Applications • Isolating wait sets in buffers, resource controllers • Designs that would otherwise encounter missed signals — Where one thread signals before the other has even started waiting — Semaphores ‘remember’ how many times they were signalled
28 .Counter Using Semaphores class BoundedCounterUsingSemaphores { long count_ = MIN; Sync decPermits _= new Semaphore(0); Sync incPermits _= new Semaphore(MAX-MIN); synchronized long value() { return count_; } void inc() throws InterruptedException { incPermits_.acquire (); synchronized(this) { ++count_; } decPermits_.release (); } void dec () throws InterruptedException { decPermits_.acquire (); synchronized(this) { --count_; } incPermits_.release (); } }
29 .Using Latches Conditions starting out false, but once set true, remain true forever • Initialization flags • End-of-stream conditions • Thread termination • Event occurrence