Lt-8 Multithreading

Intended Learning Outcomes

  • To understand the concept of concurrency.
  • To understand the difference of a process and a thread.
  • To define a thread using the Thread class and Runnable interface.
  • To control threads with various Thread methods.
  • To understand the life cycle of a thread.
  • To execute tasks in a thread pool.
  • To understand the cause of thread interference.
  • To use synchronized methods to avoid race conditions.

Concurrency:systems can do more than one thing at a time

Process and thread

  1. process:an independent program in execution with own memory space
  2. thread: a smaller unit of process, share resource and memory with other threads within the same process.

why using multithread and why not?

pros:

  • a server can handle multiple clients
  • apps more responsive to user input
  • improve the execution efficient
    cons:
  • interactions and cooperations may lead to safety and liveness problems
  • cost of thread creation

Defining and Starting Thread

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

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

Callable and Future

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]
这里有代码,记得复习。

Fork-Join Framework

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

Why race condition

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.

Atomic

If a field of type long or double is declared volatile, read and write on thatfield are also guaranteed to be atomic.

Synchronization

Using the keyword synchronized to avoid resource conflicts.

ReentrantLock

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

  • 20
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值