Intended Learning Outcomes
pros:
Runnable interface and Thread class
Runnable
Advantage: can extend other classes.
Disadvantage: less convenient if no other class is extended.
Thread
Advantage:easier to implement without additional class
Disadvantage:Just can extend one class
Thread pool consists of worker threads which will return to the pool for reuse when they
finish the job. Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.
ExecutorService is an interface for executing and managing thread tasks
Executors is a factory class for providing implementations of ExecutorService
// creates a thread pool with a fixed number of threads executing concurrently
ExecutorService pool = Executors.newFixedThreadPool(int);
// creates a thread pool that creates new threads as needed
ExecutorService pool = Executors.newCachedThreadPool();
//use a pool to execute a Runnable task
pool.execute(task);
//task:runnable
The reason why use them: the void run() method in the Runnable interface and Thread class has no return value.
So,can use Callable interface and the Future interface to help you manage thread cooperation.[Callable:capture+Future:display]
这里有代码,记得复习。
It provides tools to help speed up parallel processing by attempting to use all available processor cores.
(accomplished through a divide and conquer approach)
The forkjoin framework use a pool(forkjoinpool)(the heart of the frame)
this pool manages worker threads of type ForkJoinWorkerThread to execute subtasks ForkJoinTask.
The ForkJoinPool is an implementation of the ExcutorService that manages worker threads and provides us with tools to get information about the thread pool state and performance.
There are two related concrete subclasses of the abstract ForkJoinTask.
RecursiveAction – used to implement a task that does not yield a result
RecursiveTask – used to implement a task that yield a result
many threads access the critical region or resources at the same time to cause the race condition.
A class is said to be thread-safe if an object of the class does not cause a race condition in the presence of multiple threads.
If a field of type long or double is declared volatile, read and write on thatfield are also guaranteed to be atomic.
Using the keyword synchronized to avoid resource conflicts.
ReentrantLock is a concrete implementation of Lock interfacefor creating mutual exclusive locks. You can create a lock with the specified fairness policy.
True fairness policies guarantee the longest-wait thread to obtain thelock first.
False (default) fairness policies grant a lock to a waiting thread withoutany access order.
Programs using fair locks accessed by many threads may have worse overall performance than those using the default setting, but have smaller variances in times to obtain locks and
guarantee lack of starvation.
wait to each other release the locks