java 线程 被互斥阻塞、检查中断示例讲解----thinking java4

来源:互联网 发布:ai设计软件下载 编辑:程序博客网 时间:2024/06/06 01:23

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 9f2() calling f1() with count 8f1() calling f2() with count 7f2() calling f1() with count 6f1() calling f2() with count 5f2() calling f1() with count 4f1() calling f2() with count 3f2() calling f1() with count 2f1() calling f2() with count 1f2() 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();@Overridepublic 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 BlockedMutexIssuing 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()) {// point1NeedsCleanup n1 = new NeedsCleanup(1);// start try-finally immediately after definition// of n1 , to auarantee proper cleanup of n1try {System.out.println("sleeping");TimeUnit.SECONDS.sleep(1);// point 2NeedsCleanup n2 = new NeedsCleanup(2);// guarantee proper cleanup of n2 保证适当的清理n2try {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 1sleepingNeedsCleanup 2计算单元完成耗时的操作Cleaning up 2Cleaning up 1NeedsCleanup 1sleepingCleaning up 1exiting via inerruptedExecption*/




1 1
原创粉丝点击