Notes on Java Threads

Definition

Basics of Threads

Mechanics

The Thread class

class DemoThreads {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            // write task here
        });
        thread.start();
    }
}

The Thread Group class

Are used earlier to handle a group of threads

  • Interrup all the threads in a thread group
  • Handle uncaught exceptions
    class DemoThreads {
    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("myGroup");
        Thread thread = new Thread(threadGroup, () -> {
            // write task here
        }, "myThread");
        thread.start();
    }
    }

    Race Conditions

    If a compound operation like balance =+ amount will result in a race condition if the object is not synchronized

Race condition: is an unpredictable result from multiple threads operating the same object due to caching of object values into the register instead of reading it from memory location

Synchronized

  • built in for thread safety
  • each java object can be used as a monitor
  • only one thread at a time can acquire a monitor
  • no back off strategy to give up waiting

    
    class Account {
    private final Object monitor = new Object();
    private double balance;
    
    public Account(double amount) {
        this.balance = amount;
    }
    
    public double Deposit(double amount) {
        synchronized (monitor) {
            this.balance = this.balance + amount;
            return balance;
        }
    }
    
    public double Withdraw(double amount) {
        // rely on the synchronization of deposit
        // unnecessary locking in Withdraw
        return this.Deposit(-amount);
    }

}

#### Synchronize **this** vs **method**

```java
class Account {
    private double balance;

    public Account(double amount) {
        this.balance = amount;
    }

    public synchronized double Deposit(double amount) {
        return this.balance = this.balance + amount;
    }

    public double Withdraw(double amount) {
        synchronized (this) {
            return this.balance = this.balance - amount;
        }
    }
}

Almost the same performance results.

By synchronizing on the method you disallow the child to participate in the locking strategy.

Synchronized static methods

Locks on the class

class Singleton {
    private static Singleton instance;

    private Singleton() {
    }

    public static synchronized Singleton getInstance() {
        if (instance == null) instance = new Singleton();
        return instance;
    }
    // can be rewritten like this
//    public static Singleton getInstance(){
//        synchronized (Singleton.class){
//            if (instance == null) instance = new Singleton();
//            return instance;
//        }
//    }
}

Thread States

  • New the thread is created but not started
  • Runnable the thread started and waiting for OS to give time

    Blocked, Waiting, TimedWaiting

  • Terminated the thread exited run method

Inter-thread communication

Priorities

  • Can't exceed group priority
  • Not reliable, can be ignored by JVM or OS
0