Threads and Locks in Java

Get Started. It's Free
or sign up with your email address
Rocket clouds
Threads and Locks in Java by Mind Map: Threads and Locks in Java

1. execution

1.1. implementing Runnable or Callable interface (run method)

1.1.1. passing as a parameter to Thread, run start()

1.1.1.1. object will be able to inherit another class

1.1.1.2. no unnecessary overhead as in case of extending Thread

1.2. extending the Thread class

1.2.1. overriding run()

2. synchronization

2.1. mutual exclusive

2.1.1. synchronized methods

2.1.1.1. can be executed at the same if they are different instances of the same object

2.1.1.2. on the class lock

2.1.1.2.1. different sync methods of the same instance are not executed at the same time

2.1.1.3. static

2.1.1.3.1. if done on static method then applies to class not object

2.1.2. synchronized blocks

2.1.2.1. only one thread per instance can execute sync block

2.1.3. synchronized classes

2.2. cooperation

2.2.1. locks (=monitors, mutex)

2.2.1.1. used for more granular control

2.2.1.2. a thread gets access to a shared resource by first acquiring the lock associated with the resource

2.2.1.2.1. ReentrantLock()

2.2.2. semaphore

2.2.2.1. allows non-ownership-release semantics

2.3. states

2.3.1. diagram

2.3.2. join method

2.3.2.1. wait-notify

2.3.2.1.1. another ex

3. deadlock

3.1. occurs when (MiNCH)

3.1.1. Mutual exclusion

3.1.1.1. only one process can access a resource at a given time

3.1.1.2. if a resource has limited quantity

3.1.2. No Preemption

3.1.2.1. One process cannot forcibly remove another process' resource

3.1.3. Circular Wait

3.1.3.1. process forming a circular chain

3.1.3.2. most common to remove this condition to fix a deadlock

3.1.4. Hold and Wait

3.1.4.1. Processes already holding a resource can request more resource without relinquishing current resources

3.2. preventing

3.2.1. require a process to declare upfront what locks it will need

3.2.1.1. then check if deadlock will be created, fail if so

4. terms

4.1. process

4.1.1. instance of a program in execution

4.1.1.1. has independent memory and CPU time allocated

4.1.2. has to use inter-process communications if wants to access other process' resources

4.1.2.1. pipes

4.1.2.1.1. files

4.2. thread

4.2.1. particular execution path of a process

4.2.2. share same heap

4.2.2.1. but have own registers and stack

4.2.3. can't start twice

4.2.3.1. will throw exception

4.2.4. run() method instead of start()?

4.2.4.1. will run just as object

4.2.5. interrupt()

4.2.6. types

4.2.6.1. Daemon thread

4.2.6.2. User thread

4.3. context switch

4.3.1. time spent switching between two processes

4.4. lock ownership

4.4.1. only the thread which locked the lock can unlock it

4.4.1.1. exception if you try otherwise

4.4.1.1.1. IllegalMonitorException

4.5. mutex or lock

4.5.1. ReentrantLock and synchronized

4.6. ThreadPool

4.7. preemptive scheduling and time slicing

4.8. shutdown hook