PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   29-09-2021

Names

Threads have names (default or given by you). We don't know how often the threads will switch.
 
class LearningApp implements Runnable {
        
    public static void main(String[] args) {

        LearningApp runner = new LearningApp();
       
        Thread alpha = new Thread(runner);
        Thread beta = new Thread(runner);
        
        alpha.setName("Alpha thread");
        //beta.setName("Beta thread");
        
        alpha.start();
        beta.start();
    }
    
    @Override
    public void run() {

        for (int i=0; i<125; i++) {

            System.out.println(
                Thread.currentThread().getName()
            );
        }
    }
}

/* Output:

    Alpha thread
    Alpha thread
    ...
    Thread-1
    Thread-1
    ...
*/
... 21 lines
 

Concurency

p38 Two or more threads may have access to a single object's data. We need a lock for access.
 
class LearningApp implements Runnable {
            
    private BankAccount account = new BankAccount();
    
    public static void main(String[] args) {
    
        LearningApp runner = new LearningApp();
        
        Thread alpha = new Thread(runner);
        Thread beta = new Thread(runner);

        alpha.setName("Alpha");
        beta.setName("Beta");

        alpha.start();
        beta.start();
    }
    
    @Override
    public void run() {

        for (int i=0; i<2; i++) {
            
            System.out.println(
                "Account balance: " + account.getBalance()
            );            
            makeWithdraw(10);
        }
    }
    
    private void makeWithdraw(int amount) {

        if (account.getBalance() >= amount) { 

            try {
                System.out.println(
                    Thread.currentThread().getName() + 
                        " is going to sleep"
                );
                Thread.sleep(500); // sleep for account lock

            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            }
            
            account.withdraw(amount);
        }
    }
}

class BankAccount {
    
    private int balance = 20;
    
    public int getBalance() { // current account balance

        return balance;
    }
    
    public void withdraw(int amount) { // withdraw from account

        balance = balance - amount;

        System.out.println(
            Thread.currentThread().getName() + " withdraw 10"
        );
    }
}

/* Outputs:

    Account balance: 20
    Account balance: 20
    Beta is going to sleep
    Alpha is going to sleep

    Beta withdraw 10
    Alpha withdraw 10
    Account balance: 0
    Account balance: 0

    OR

    Account balance: 20
    Account balance: 20
    Beta is going to sleep
    Alpha is going to sleep

    Beta withdraw 10
    Account balance: 0
    Alpha withdraw 10
    Account balance: 0
*/
... 60 lines
 

Synchronized

p38 Only one thread at a time can access the method.
 
class LearningApp implements Runnable {
    
    private BankAccount account = new BankAccount();
    
    public static void main(String[] args) {
    
        LearningApp runner = new LearningApp();
        
        Thread alpha = new Thread(runner);
        Thread beta = new Thread(runner);

        alpha.setName("Alpha");
        beta.setName("Beta");

        alpha.start();
        beta.start();
    }
    
    @Override
    public void run() {

        for (int i=0; i<2; i++) {

            System.out.println(
                "Account balance: " + account.getBalance()
            );

            makeWithdraw(10);
        }
    }
    
    // LOOK HERE

    private synchronized void makeWithdraw(int amount) {

        if (account.getBalance() >= amount) {

            try {
                System.out.println(
                    Thread.currentThread().getName() 
                        + " is going to sleep"
                );
                Thread.sleep(500); 

            } catch (InterruptedException e) { 
                e.printStackTrace();
            }

            account.withdraw(amount);
        }
    }
}

class BankAccount {
    
    private int balance = 20;
    
    public int getBalance() {
        return balance;
    }
    
    public void withdraw(int amount) {
        balance = balance - amount;
        System.out.println(
            Thread.currentThread().getName() + " withdraw 10"
        );
    }
}

/* Outputs: (now is correct)

    Account balance: 20
    Account balance: 20

    Beta is going to sleep
    Beta withdraw 10
    Account balance: 10

    Alpha is going to sleep
    Alpha withdraw 10

    Account balance: 0
*/
... 52 lines
 

Questions    
Scheduler

        A B C D E F
🔔
1/3