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
- java 线程 被互斥阻塞、检查中断示例讲解----thinking java4
- java 线程 在阻塞时终结 之中断讲解 ---thinking in java4
- java 线程 生产者-消费者与队列,任务间使用管道进行输入、输出 讲解示例 --thinking java4
- java 线程 原子类相关操作示例 thinking in java4 目录21.3.4
- java线程 同步临界区:thinking in java4 21.3.5
- java线程 在其他对象上同步、线程本地存储ThreadLocal:thinking in java4 21.3.6
- java 线程 阻塞中断 InterrupedtException
- Java线程阻塞与中断
- java线程共享受限资源 解决资源竞争 thinking in java4 21.3
- Java中用interrupt()方法中断阻塞线程
- Java线程阻塞中断和LockSupport问题
- Java线程阻塞中断和LockSupport问题
- Java 线程阻塞、中断及优雅退出
- Thinking in java4 容器
- Thinking in java4 Exception
- java线程阻塞中断与LockSupport使用介绍
- Java线程阻塞中断和LockSupport的常见问题
- java线程阻塞中断和LockSupport的常见问题
- 我的emacs配置
- C++11的function bind回调机制
- 虚函数表的实现细节
- 翻译:Laravel-4-Generators 使用自定义代码生成工具快速进行Laravel开发
- Selenium api使用说明
- java 线程 被互斥阻塞、检查中断示例讲解----thinking java4
- nginx负载均衡
- C++的输入和输出与标准输出流
- JAVA socket编程实例(标注)
- 获得android手机屏幕的宽高的类
- gnuplot配置HOME目录
- hive-0.12安装详解
- Text Justification
- 如果你做的事情毫不费力,就是在浪费时间