zoukankan      html  css  js  c++  java
  • About the diffrence of wait timed_wait and block in java

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * 
     * @author daxin
     *
     */
    public class Main1 {
    
    	static Lock lock = new ReentrantLock();
    
    	public static void main(String[] args) throws Exception {
    
    		// TIMED_WAITING
    		// Thread.sleep(5000*5000);
    
    		// ----------------synchronized---------------------
    		// TIMED_WAITING
    		new Thread(target1, "1").start();
    		Thread.sleep(200);
    		// BLOCKED
    		new Thread(target1, "2").start();
    
    		// ----------------lock------------------
    		// TIMED_WAITING
    		new Thread(target2, "3").start();
    		Thread.sleep(200);
    		// WAITING
    		new Thread(target2, "4").start();
    
    	}
    
    	public static synchronized void get() {
    
    		try {
    			Thread.sleep(5000 * 5000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    
    		}
    	}
    
    	public static void get2() {
    
    		try {
    			lock.lock();
    			Thread.sleep(5000 * 5000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			lock.unlock();
    		}
    	}
    
    	static Runnable target1 = new Runnable() {
    
    		@Override
    		public void run() {
    			// TODO Auto-generated method stub
    			get();
    		}
    	};
    
    	static Runnable target2 = new Runnable() {
    
    		@Override
    		public void run() {
    			// TODO Auto-generated method stub
    			get2();
    		}
    	};
    
    }
    
  • 相关阅读:
    VS中,如何将存在于解决方案里,但是没有显示出来的文件(或文件夹)显示到项目中。
    (转)分布式系统编程,你到哪一级了?
    领域驱动架构学习总结
    Java多线程总结
    MySQL更新优化
    MySQL查询优化
    MySQL索引
    MySQL优化
    MySQL联接操作
    MySQL架构
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7417304.html
Copyright © 2011-2022 走看看