zoukankan      html  css  js  c++  java
  • java 线程 被相互排斥堵塞、检查中断演示样例解说----thinking java4

    package org.rui.thread.block;
    
    /**
     * 被相互排斥堵塞 就像在interrupting.java中看到的,假设你偿试着在一个对象上调用其synchronized方法,
     * 而这个对象的锁已经被其它任务获得,那么调用任务将被挂起(堵塞) ,直至这个锁可获得. 
     * 以下的演示样例说明了同一个相互排斥能够怎样能被同一个任务多次获得
     * 
     * @author lenovo
     * 
     */
    public class MultiLock {
    	public synchronized void f1(int count) {
    		if (count-- > 0) {
    			System.out.println("f1() calling f2() with count " + count);
    			f2(count);
    		}
    	}
    
    	public synchronized void f2(int count){
    		if(count-->0){
    			System.out.println("f2() calling f1() with count "+count);
    			f1(count);
    		}
    	}
    	
    	public static void main(String[] args) {
    		final MultiLock multiLock=new MultiLock();
    		new Thread(){
    			public void run(){
    				multiLock.f1(10);
    			}
    		}.start();
    	}
    }
    /**OUTPUT:
    f1() calling f2() with count 9
    f2() calling f1() with count 8
    f1() calling f2() with count 7
    f2() calling f1() with count 6
    f1() calling f2() with count 5
    f2() calling f1() with count 4
    f1() calling f2() with count 3
    f2() calling f1() with count 2
    f1() calling f2() with count 1
    f2() calling f1() with count 0
    */

    package org.rui.thread.block;
    
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    //Mutex  相互排斥      Reentrant :可重入
    class BlockedMutex {
    	private Lock lock = new ReentrantLock();
    
    	public BlockedMutex() {
    		// Acquire it reght away, to demonstrate interruption 获取它心中,来演示中断
    		// of a task blocked on a ReentrantLock reentrantLock的任务了
    		lock.lock();
    	}
    
    	public void f() {
    		try {
    			// this will nerer be available to a second task 这将纵然是可用的第二个任务
    			lock.lockInterruptibly();// 假设当前线程未被中断,则获取锁     special call 
    			System.out.println("lock acquired in f()");
    		} catch (InterruptedException e) {
    			System.out.println("interrupted from lock acuisition in f()");
    		}
    	}
    }
    
    class Blocked2 implements Runnable {
    	BlockedMutex blocked = new BlockedMutex();
    
    	@Override
    	public void run() {
    		System.out.println("Waiting for f()  in BlockedMutex");
    		blocked.f();
    		System.out.println("Broken out of blocked call");//爆发的堵塞调用
    
    	}
    
    }
    
    public class Interruptiing2 {
    	public static void main(String[] args) throws InterruptedException {
    		Thread t=new Thread(new Blocked2());
    		t.start();
    		TimeUnit.SECONDS.sleep(1);
    		System.out.println("Issuing t.interrupt()");
    		//t.interrupt();//中断线程
    	}
    }
    /**
     * output:
    Waiting for f()  in BlockedMutex
    Issuing t.interrupt()
    interrupted from lock acuisition in f()
    Broken out of blocked call
     */
    



    package org.rui.thread.block;
    
    import java.util.concurrent.TimeUnit;
    /**
     * 检查中断
     * @author lenovo
     *
     */
    class NeedsCleanup {//须要清除
    	private final int id;
    
    	public NeedsCleanup(int ident) {
    		id = ident;
    		System.out.println("NeedsCleanup " + id);
    	}
    
    	public void cleanup() {
    		System.out.println("Cleaning up " + id);
    	}
    }
    
    class Blocked3 implements Runnable {
    	private volatile double d = 0.0;
    	
    	public void run() {
    		try {
    			while (!Thread.interrupted()) {
    				// point1
    				NeedsCleanup n1 = new NeedsCleanup(1);
    				// start try-finally immediately after definition
    				// of n1 , to auarantee proper cleanup of n1
    				try {
    					System.out.println("sleeping");
    					TimeUnit.SECONDS.sleep(1);
    					// point 2
    					NeedsCleanup n2 = new NeedsCleanup(2);
    					// guarantee proper cleanup of n2 保证适当的清理n2
    					try {
    						System.out.println("计算单元");
    						// A time-consuming,non-blocking operation:  耗时,非堵塞操作
    						for (int i = 1; i < 2500000; i++) {
    							d = d + (Math.PI + Math.E) / d;
    						}
    						System.out.println("完毕耗时的操作");
    					} finally {
    						n2.cleanup();
    					}
    
    				} finally {
    					n1.cleanup();
    					//throw new InterruptedException();
    				}
    			}
    			System.out.println("exiting via while() test");
    		} catch (InterruptedException e) {
    			System.out.println("exiting via inerruptedExecption");
    		}
    	}
    }
    
    // /////////////////////////////////////
    
    public class InterruptingIdiom {
    
    	public static void main(String[] args) throws Exception {
    		String[] arg = { "1100" };
    		if (arg.length != 1) {
    			System.exit(1);
    		}
    		Thread t = new Thread(new Blocked3());
    		t.start();
    		TimeUnit.MILLISECONDS.sleep(new Integer(arg[0]));
    		t.interrupt();
    	}
    }
    /**
    output:
    
    NeedsCleanup 1
    sleeping
    NeedsCleanup 2
    计算单元
    完毕耗时的操作
    Cleaning up 2
    Cleaning up 1
    NeedsCleanup 1
    sleeping
    Cleaning up 1
    exiting via inerruptedExecption
    */




  • 相关阅读:
    xamp配置多域名站点
    POJ1611-The Suspects-ACM
    POJ2524-宗教问题-并查集-ACM
    POJ3274-牛的属性-HASH-ACM
    拓扑排序-DFS
    拓扑排序
    POJ1007-DNA Sorting-ACM
    POJ1258-Agri-Net-ACM
    wdcp-apache配置错误导致进程淤积进而内存吃紧
    wdcp-apache开启KeepAlive提高响应速度
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7269183.html
Copyright © 2011-2022 走看看