• 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();
    		}
    	};
    
    }
    
  • 相关阅读:
    境外支付宝接口对接--支付接口
    js bind
    css的input文本框的 propertychange、focus、blur
    字符流Reader和Writer
    对象流--对象的序列化
    输入流IS和输出流OS学习总结
    File文件的读写操作RandomAccessFile类
    File文件操作学习总结
    Map集合的便利学习总结
    Map集合学习总结
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7417304.html
Copyright © 2020-2023  润新知