Print
Category: IT
Hits: 632

Reasons for multithreading:

Problems caused by improperly implemented thread programming

Race Condition - two threads try to update variable at the same time

Deadlock - two threads cannot proceed because each one is waiting on a lock the other one is holding

Overlock - keep locking when performing other unrelated functions

Types of sync objects:

Any Java object

Object monitor;
synchronized (monitor) {monitor.set();....};

Any Class

synchronized (this.getClass()) {set();....};

Method

synchronized public void func (){...};

wait and notify

synchronize on an object is called having a monitor on the object

    synchronized (obj) {
            obj.wait();
        }
    }

    synchronized (obj) {
            obj.notifyAll();
        }
    }

concurrent.Semaphore

semaphore.acquire();
semaphore.release();

concurrent.atomic.*

Wrappers for primitive types that support concurrent update

Declare Volatile

Read and write to volatile is ALWAYS synchronized.

 

Optimistic Locking vs Pessimistic Locking

Optimistic Locking

Pessimistic Locking

Process isolation and concurrency

Deadlock

Method exposed to deadlock

int func() {

    ++n;
    ++n;
    return n; 

}

when executed from multiple threads may not always return an even increment

 

Concurrent package:

atomic:  wrapper classes allowing access as if it is volatile
            unlike Java wrapper has nave no hashCode() or compareTo()

lock:      reentrant locks, multithread access prioritized by starvation time

blocking queue: insert is blocked until remove is called and vice versa