cs notebook1
  • Introduction
  • sort algorithm
  • Overloading VS Overriding
  • multithreading
  • Concurrency0
  • Concurrency1
  • ExecutorService
  • iteration & recursion
  • IO
  • Marker interface(Serializable, Clonnable&Remote)
  • Jackson Library
  • java.lang.System.*
  • Virtual Memory
  • Java ClassLoader
  • interfaceVSabstractClass
  • ENUM
Powered by GitBook
On this page
  • 7 Java Volatile Keyword
  • 7.1 Volatile keyword
  • 7.2 Happens-Before guarantee
  • 7.3 volatile is Not Always Enough
  • 7.4 performance
  • 8 Java ThreadLocal
  • 8.1 When to use the ThreadLocal
  • 9 Thread Signaling
  • 9.1 wait(), notify()¬ifyAll()
  • 9.2 Don't call wait() on constant String's or global object
  • 10 Deadlock
  • 10.1 Database Deadlocks
  • 11 Deadlock Prevention
  • 11.1 Lock Ordering
  • 11.2 Lock Timeout
  • 11.3 Deadlock Detection
  • 12 Starvation and Fairness
  • 12.1 Causes of Starvation in Java
  • 12.2 Implementing Fairness in Java
  • 13 Nested Monitor Lockout
  • Nested Monitor Lockout VS Deadlock
  • 14 Slipped Conditions
  • 15 Locks in Java
  • 15.1 Main Differences Between Locks and Synchronized Blocks
  • 16 Semaphores

Was this helpful?

Concurrency1

PreviousConcurrency0NextExecutorService

Last updated 6 years ago

Was this helpful?

7 Java Volatile Keyword

7.1 Volatile keyword

every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.

By declaring the counter variable volatile all writes to the counter variable will be written back to main memory immediately. Also, all reads of the counter variable will be read directly from main memory.

7.2 Happens-Before guarantee

When a thread writes to a volatile variable, then not just the volatile variable itself is written to main memory. Also all other variables changed by the thread before writing to the volatile variable are also flushed to main memory. When a thread reads a volatile variable it will also read all other variables from main memory which were flushed to main memory together with the volatile variable.

7.3 volatile is Not Always Enough

7.4 performance

Thus, you should only use volatile variables when you really need to enforce visibility of variables.

8 Java ThreadLocal

The ThreadLocal class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal variable, then the two threads cannot see each other's ThreadLocal variable.

As its name suggests, a single instance of ThreadLocal can store different values for each thread independently. Therefore, the value stored in a ThreadLocal instance is specific (local) to the current running thread and any other code logic running on the same thread will see the same value, but not the values set on the same instance by other threads. (There are exceptions, like InheritableThreadLocal, which inherits its parent thread’s values by default.)

8.1 When to use the ThreadLocal

I can point out one use case where I used thread local. Consider you have a Servlet which calls some business methods. You have a requirement to generate a unique transaction id for each and every request this servlet process and you need to pass this transaction id to the business methods, for logging purpose. One solution would be passing this transaction id as a parameter to all the business methods. But this is not a good solution as the code is redundant and unnecessary.

To solve that, you can use Thread Local. You can generate a transaction id (either in servlet or better in a filter) and set it in the Thread Local. After this, what ever the business method, that this servlet calls, can access the transaction id from the thread local.

This servlet might be servicing more that one request at a time. Since each request is processed in separate thread, the transaction id will be unique to each thread (local) and will be accessible from all over the thread’s execution (global).

9 Thread Signaling

9.1 wait(), notify()¬ifyAll()

9.2 Don't call wait() on constant String's or global object

The problem with calling wait() and notify() on the empty string, or any other constant string is, that the JVM/Compiler internally translates constant strings into the same object. That means, that even if you have two different MyWaitNotify instances, they both reference the same empty string instance. This also means that threads calling doWait() on the first MyWaitNotify instance risk being awakened by doNotify() calls on the second MyWaitNotify instance.

10 Deadlock

A deadlock is when two or more threads are blocked waiting to obtain locks that some of the other threads in the deadlock are holding. Deadlock can occur when multiple threads need the same locks, at the same time, but obtain them in different order.

10.1 Database Deadlocks

11 Deadlock Prevention

11.1 Lock Ordering

Deadlock occurs when multiple threads need the same locks but obtain them in different order.

If you make sure that all locks are always taken in the same order by any thread, deadlocks cannot occur.

11.2 Lock Timeout

Another deadlock prevention mechanism is to put a timeout on lock attempts meaning a thread trying to obtain a lock will only try for so long before giving up. If a thread does not succeed in taking all necessary locks within the given timeout, it will backup, free all locks taken, wait for a random amount of time and then retry. The random amount of time waited serves to give other threads trying to take the same locks a chance to take all locks, and thus let the application continue running without locking.

11.3 Deadlock Detection

Deadlock detection is a heavier deadlock prevention mechanism aimed at cases in which lock ordering isn't possible, and lock timeout isn't feasible.

Every time a thread takes a lock it is noted in a data structure (map, graph etc.) of threads and locks. Additionally, whenever a thread requests a lock this is also noted in this data structure.

A better option is to determine or assign a priority of the threads so that only one (or a few) thread backs up. The rest of the threads continue taking the locks they need as if no deadlock had occurred. If the priority assigned to the threads is fixed, the same threads will always be given higher priority. To avoid this you may assign the priority randomly whenever a deadlock is detected.

12 Starvation and Fairness

12.1 Causes of Starvation in Java

  • Threads with high priority swallow all CPU time from threads with lower priority.

  • Threads are blocked indefinitely waiting to enter a synchronized block, because other threads are constantly allowed access before it.

  • Threads waiting on an object (called wait() on it) remain waiting indefinitely because other threads are constantly awakened instead of it.

12.2 Implementing Fairness in Java

  • using Locks instead of Synchronized Blocks

  • a fair lock

13 Nested Monitor Lockout

Nested Monitor Lockout VS Deadlock

In deadlock, two threads are waiting for each other to release locks.

In nested monitor lockout, Thread 1 is holding a lock A, and waits for a signal from Thread 2. Thread 2 needs the lock A to send the signal to Thread 1.

14 Slipped Conditions

15 Locks in Java

to be continue...

15.1 Main Differences Between Locks and Synchronized Blocks

  • A synchronized block makes no guarantees about the sequence in which threads waiting to entering it are granted access.

  • You cannot pass any parameters to the entry of a synchronized block. Thus, having a timeout trying to get access to a synchronized block is not possible.

  • The synchronized block must be fully contained within a single method. A Lock can have it's calls to lock() and unlock() in separate methods.

16 Semaphores

Semaphore semaphore = new Semaphore(5);

We are using a semaphore of size 5, thus limiting concurrent access to 5.

https://dzone.com/articles/painless-introduction-javas-threadlocal-storage
http://tutorials.jenkov.com/java-concurrency/threadlocal.html
http://tutorials.jenkov.com/java-concurrency/deadlock-prevention.html