標誌位未釋放造成的死鎖
public class Utils {
private static Logger logger = LoggerFactory.getLogger(Utils.class);
public static boolean used=false;
public synchronized static void work(){
if(!used){
try {
logger.info("work start");
used=true;
Thread.sleep(3000);
System.out.println(0/0);
logger.info("work end");
} catch (InterruptedException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}else{
logger.info("資源正在使用中");
}
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 3; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
work();
}
});
}
}
}