Concurrent

Thread

Why need thread?

  • Sometimes, we need to run the task on background, such as: generating report, getting data from database, we don't want to do this in the main thread instead of doing it by creating a new thread

Cycle

  • We need to create a new thread , runnable class which is responsible for executing the task

Method

Join

  • Wait for other thread to finish for several time

Interrupt

  • To wake the sleeping thread up

Racing Condition

  • As threads shares the same heap, if threads access the same objects at the same time, racing condition/ thread interference will be occur. We cannot ensure the order of event

Output
  • Countdown Object is accessed by threads at the same time, racing condition is happened

Synchronization

  • To make a scope of the code to ensure that one thread can run, other thread keep waiting until releasing the scope by thread

Output

DeadLock

  • The task of 2 threads rely on the release of the lock to finish task at the same time, therefore, the task of 2 threads cannot be finished, the object lock will be kept forever

LiveLock

  • The task of 2 threads will keep looping and failed to release the lock

Output

Producer & Consumer

  • As we need to make a order that receiving function should wait for the sending function,

  • we can make good use of wait and notify, when notify is not received, the task will stop in while loop

  • Until notify is made, the loop will be break and keep running the process

  • Wait and notify can only be used within synchronized scope

  • Wait should be used in while loop

Thread Starvation

  • Assume that there are many threads, by using synchronized scope, the task of random thread will be chosen to run when a thread is finished

  • However, there will be possibility that a thread will be wait for longer and longer and never be chosen to run unluckily

  • In such a case, we can make good use of fair lock to replace the synchronized scope

  • The thread having longest suspending time will be chosen to run automatically

  • From the result, we can see that the order is based on waiting time

  • Unlike synchronization, we must need to add back unlock so as to release to another thread

Executor

Executor

  • Executor provides a flexibility to run the task in main thread directly or create a new thread to run the task

Executors

  • It is a factory class which provide a template for creating executor based on the thread pool

ExecutorService

  • It is class extended from executor class, which provide additional method - submit which can return a future as a result

  • However, the executor service still running even the task is finished and wait for accepting new task, so we need to shutdown the thread manually

ThreadPoolTaskExecutor

  • It is a class extended from executor class, we can easily customize the thread setting on that

Thread Pool

  • Used to control then number of task can be run at the same time

  • Core Pool Size: The number of thread created in the beginning

  • Queue Capacity: The number of thread that can be suspended

  • Max Pool Size: The max number of thread that can be run at the same time

  • When the number of thread is exceed the core pool size , the task of thread will be suspended , put in the query

  • The query is full, the new thread pool will be created so as to run the task of the thread, and the position of query will also be released

  • If the total number of thread created is larger than the queue capacity and the max pool size, the task of the thread will be rejected and throw the exception

  • The setting of core pool size can be 0 and cannot larger than the max pool size

Future

  • It is the return value of a thread

  • It can be used the stop the task even it is running and check whether the task is cancelled or finished

AtomicVariable & ConcurrentHashMap

  • Use them when the variable have chance to be changed by multiple threads at the same time

  • It is thread-safe even without using synchronization scope

Last updated

Was this helpful?