Thursday, June 19, 2014

Swotting threads in Java

Threads are sometimes called lightweight processes

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 long and double).
  • Reads and writes are atomic for all variables declared volatile (including long and double variables).
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 ScheduledExecutorService Interface

Trhead 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 on
recursive task and recursive action
Java 8 uses frosk join on mutiple processors in a "parallelSort" in Arrays Class - leverages concurrency and processors
fork join is use in the lamda framework.
Home Page > Essential Classes > Concurrency

Concurrent Collections

The java.util.concurrent package includes a number of additions to the Java Collections Framework. These are most easily categorized by the collection interfaces provided:
  • BlockingQueue defines 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.
  • ConcurrentMap is a subinterface of java.util.Map that 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 of ConcurrentMap is ConcurrentHashMap, which is a concurrent analog of HashMap.
  • ConcurrentNavigableMap is a subinterface of ConcurrentMap that supports approximate matches. The standard general-purpose implementation of ConcurrentNavigableMap is ConcurrentSkipListMap, which is a concurrent analog of TreeMap.
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