zoukankan      html  css  js  c++  java
  • 3.线程死锁

    死锁:同步锁中嵌套同步锁,导致锁无法释放
     
    package com.jlong;
     
    class ThreadTrain4 implements Runnable {
        private static int count = 100;
        public  Object object = new Object();
        public boolean flag = true;
     
        @Override
        public void run() {
            if (flag) {
                while (count > 0) {
                    synchronized (object) {
                        show();
                    }
                }
            } else {
                while (count > 0) {
                    show();
                }
            }
        }
     
        public synchronized void show() {
            synchronized (object){
                if (count > 0) {
                    try {
                        Thread.sleep(50);
     
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "出售的第" + (100 - count + 1) + "张票");
                    count--;
     
                }
            }
     
        }
    }
     
    public class ThreadDemo4 {
        public static void main(String[] args) throws InterruptedException {
            ThreadTrain4 threadTrain = new ThreadTrain4();
            Thread thread1 = new Thread(threadTrain);
            Thread thread2 = new Thread(threadTrain);
            thread1.start();
            Thread.sleep(50);
            threadTrain.flag = false;
            thread2.start();
        }
    }
  • 相关阅读:
    Transaction And Lock--使用资源锁来控制并发
    曲演杂坛--页拆分
    维护建议--文件和文件组
    维护建议--开发设计
    维护建议--服务器磁盘
    维护建议--数据库备份
    TSQL--查找连续登陆用户
    shell脚本传递带有空格的参数的解决方法
    oozie无法识别hadoopHA中的ns1
    ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf
  • 原文地址:https://www.cnblogs.com/goldlong/p/10953840.html
Copyright © 2011-2022 走看看