subclass thread
implement runnable - pass it to a thread object
Thread class has static methods - tools for threading
Sleep - to millis and nanos -suspends execution
Thread. interupted
Join - if ont thread calls another threads, join - it waits for theat thread to complete before continueing.
Synchronization - overhead
synch on method locks class
suynch on code locks a local object - faster - more complex
need to make it findegrained
- Reads and writes are atomic for reference variables and for most primitive variables (all types except
longanddouble). - Reads and writes are atomic for all variables declared
volatile(includinglonganddoublevariables).
Atomic actions cannot be interleaved, so they can be used without fear of thread interference.
deadlock, starvation, livelock
loops needs waits
More sophisticated locking idioms are supported by the
java.util.concurrent.locks package.private final Lock lock = new ReentrantLock();
The biggest advantage of Lock objects over implicit locks is their ability to
back out of an attempt to acquire a lock. The tryLock method backs out if the
lock is not available immediately or before a timeout expires (if specified).
----
there's a close connection between the task being done by a new thread, as defined by its
Runnable object, and the thread itself, as defined by a Thread object. This works well for small applications, but in large-scale applications, it makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors
The Executor Interface =
e.execute(r); - r is a runnable
The
ScheduledExecutorServiceInterfaceTrhead pool - specifed number fo threads active, pulling ona queue of tasks/events.Java 7 has fork/join - work stealing - divides work, waits for result, carries onrecursive task and recursive actionJava 8 uses frosk join on mutiple processors in a "parallelSort" in Arrays Class - leverages concurrency and processorsfork join is use in the lamda framework.Concurrent CollectionsHome Page > Essential Classes > ConcurrencyConcurrent Collections
Thejava.util.concurrentpackage includes a number of additions to the Java Collections Framework. These are most easily categorized by the collection interfaces provided:
BlockingQueuedefines a first-in-first-out data structure that blocks or times out when you attempt to add to a full queue, or retrieve from an empty queue.ConcurrentMapis a subinterface ofjava.util.Mapthat defines useful atomic operations. These operations remove or replace a key-value pair only if the key is present, or add a key-value pair only if the key is absent. Making these operations atomic helps avoid synchronization. The standard general-purpose implementation ofConcurrentMapisConcurrentHashMap, which is a concurrent analog ofHashMap.ConcurrentNavigableMapis a subinterface ofConcurrentMapthat supports approximate matches. The standard general-purpose implementation ofConcurrentNavigableMapisConcurrentSkipListMap, which is a concurrent analog ofTreeMap.All of these collections help avoid Memory Consistency Errors by defining a happens-before relationship between an operation that adds an object to the collection with subsequent operations that access or remove that object.
No comments:
Post a Comment