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() object will be able to inherit another class 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 can be executed at the same if they are different instances of the same object on the class lock different sync methods of the same instance are not executed at the same time static if done on static method then applies to class not object

2.1.2. synchronized blocks only one thread per instance can execute sync block

2.1.3. synchronized classes

2.2. cooperation

2.2.1. locks (=monitors, mutex) used for more granular control a thread gets access to a shared resource by first acquiring the lock associated with the resource ReentrantLock()

2.2.2. semaphore allows non-ownership-release semantics

2.3. states

2.3.1. diagram

2.3.2. join method wait-notify another ex

3. deadlock

3.1. occurs when (MiNCH)

3.1.1. Mutual exclusion only one process can access a resource at a given time if a resource has limited quantity

3.1.2. No Preemption One process cannot forcibly remove another process' resource

3.1.3. Circular Wait process forming a circular chain most common to remove this condition to fix a deadlock

3.1.4. Hold and Wait 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 then check if deadlock will be created, fail if so

4. terms

4.1. process

4.1.1. instance of a program in execution has independent memory and CPU time allocated

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

4.2. thread

4.2.1. particular execution path of a process

4.2.2. share same heap but have own registers and stack

4.2.3. can't start twice will throw exception

4.2.4. run() method instead of start()? will run just as object

4.2.5. interrupt()

4.2.6. types Daemon thread 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 exception if you try otherwise 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