---
# System prepended metadata

title: Java AIDL

---

# Java AIDL
### 結束線程
```java
Thread thread = new Thread() {
    @Override
    public void run() {
        for (int i = 0; i < 1000000; i++) {
            //Thread.interrupted() 結束後下次返回false
            if(isInterrupted()) {
                //擦屁股
                return;
            }
            System.out.println("number: " + i);
        }
    }
};
thread.start();

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

//標記線程中斷 
thread.interrupt();
```

### 為甚麼需要 InterruptedException
因為 sleep, join, wait 不操作資源 所以本身線程在 Sleep 被外部調用 interrupt 會直接 Exception
```java
Thread thread = new Thread() {
    @Override
    public void run() {
    
        //不須拋異常的寫法
        //SystemClock.sleep(1000);
        
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            //擦屁股
            e.printStackTrace();
        }
    }
};
thread.start();

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

//標記線程中斷 
thread.interrupt();
```

### wait + notify(必定配合)
#### 寫在 synchronized 裡面 並且都是 monitor 去驅動
```java
private String sharedString;

private synchronized void initString() {
    sharedString = "wade";
    //notify(); //資料改變 通知等待區的去排隊
    notifyAll(); //資料改變 通知等待區的去排隊 避免有其他線程 全部都起來
}

private synchronized void printString() {
    while (sharedString == null) {
        try {
            wait(); //不拿鎖 去等待區
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("String: " + sharedString);
}

public void main() {
    Thread thread1 = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            printString();
        }
    };
    thread1.start();

    Thread thread2 = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            initString();
        }
    };
    thread2.start();
}
```

### join
#### 加上 join 類似於 wait 不需要添加 notify 必須等 join 的線程完成才會往下跑
```java
private String sharedString;

private synchronized void initString() {
    sharedString = "wade";
}

private synchronized void printString() {
    System.out.println("String: " + sharedString);
}

public void main() {
    Thread thread2 = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            initString();
        }
    };
    thread2.start();

    Thread thread1 = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                thread2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            printString();
        }
    };
    thread1.start();
}
```

### yield
#### 短暫讓出執行 使用場景非常少
