- –阻塞
- –I/O: 可能线程在进行一个大数据的I/O操作, 导致程序看上去假死,导致的阻塞
- –sleep(): 线程处于休眠状态
- –lock: 等待锁资源的时候
- –yield: 主动将CPU时间片释放
–死锁案例
package com.ygq.thread;
public class DeadLock {
private static String fileA = "A文件";
private static String fileB = "B文件";
public static void main(String[] args) {
new Thread(){
public void run(){
while (true){
synchronized (fileA) {
System.out.println(this.getName() + ":文件A写入");
synchronized (fileB){
System.out.println(this.getName() + ":文件B写入");
}
System.out.println(this.getName() + "所有文件保存");
}
}
}
}.start();
new Thread(){
public void run(){
while (true){
synchronized (fileB) {
System.out.println(this.getName() + ":文件B写入");
synchronized (fileA){
System.out.println(this.getName() + ":文件A写入");
}
System.out.println(this.getName() + "所有文件保存");
}
}
}
}.start();
}
}