> For the complete documentation index, see [llms.txt](https://amartyushov.gitbook.io/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://amartyushov.gitbook.io/tech/programming-languages/java/coursera-concurrent-programming.md).

# Coursera: concurrent programming

## Thread lifecycle

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FNFgRE4Slyqb0LMunbGce%2FScreenshot%202022-09-12%20at%2008.41.42.png?alt=media&amp;token=e162b0d5-4d21-4ffd-9693-77bb22989312" alt=""><figcaption></figcaption></figure>

### wait() method

Simply put, calling *wait()* forces the current thread to **wait until some other thread** invokes ***notify()*** or ***notifyAll**()* on the **same object**. For this, the current thread must own the object's [monitor](https://www.baeldung.com/cs/monitor). According to [Javadocs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#notify\(\)), this can happen in the following ways:

* when we've executed *synchronized* instance method for the given object
* when we've executed the body of a *synchronized* block on the given object
* by executing *synchronized static* methods for objects of type *Class*

{% hint style="warning" %}
WAIT() causes the current thread to place itself in the wait set for this object and then to give away any and all synchronization claims on this object. Note that only the locks on this object are given away; any other objects on which the current thread may be synchronized remain locked while the thread waits.
{% endhint %}

### **notify() and notifyAll()**

For all threads waiting on this object's monitor (by using any one of the *wait()* methods), the method *notify()* notifies any one of them to wake up arbitrarily. The choice of exactly which thread to wake is nondeterministic and depends upon the implementation.

Since *notify()* wakes up a single random thread, we can use it to implement mutually exclusive locking where threads are doing similar tasks. But in most cases, it would be more viable to implement *notifyAll()*.

## **Critical section**

*Critical sections* and the *isolated* construct can help concurrent threads manage their accesses to shared resources, at a higher level than just using locks. When programming with threads, it is well known that the following situation is defined to be a *data race* error — when two accesses on the same shared location can potentially execute in parallel, with least one access being a write. However, there are many cases in practice when two tasks may legitimately need to perform concurrent accesses to shared locations, as in the bank transfer example.

## Optimistic concurrency

```java
AtomicInteger;

fun getAndSet(delta){
    // repeat this logic in while loop until succeed
    current = this.get()
    nextValue = current + delta
    this.compareAndSet(nextValue) // if thread realises that current value is different
                                // set will not happen
}
```
