Java thread is blocked by mutex, check interrupt example explained----thinking JAVA4

Source: Internet
Author: User
Tags lenovo

Package org.rui.thread.block;/** * Blocked by mutex as seen in Interrupting.java, if you try to invoke its synchronized method on an object, the lock of this object has been obtained by other tasks,  Then the calling task will be suspended (blocked) until the lock is available.  * The following example illustrates how the same mutex can be obtained multiple times by the same task * * @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 () {Multiloc K.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 C Ount 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 Mutex reentrant: Can be re-entered class Blockedmutex {private lock lock = new REENTR Antlock ();p ublic Blockedmutex () {//Acquire it reght away, to demonstrate interruption get it in mind, to demonstrate interrupt//of a task blocked on A Reentrantlock Reentrantlock's mission was Lock.lock ();} public void F () {try {//This will Nerer is available to a second task which would be available even though the second task lock.lockinterruptibly ();//If the current thread is not interrupted , then get the lock 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.prin TLN ("Waiting for f () in Blockedmutex"); BLOCKED.F (); System.out.println ("Broken out of the blocked call");//outbreak of blocking calls}}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 ();//Interrupt Thread}}/** * output:waiting for F () in Blockedmutexissuing T.interrupt () interrupted from lock acuisition in F () broken off of blocked call * *



Package Org.rui.thread.block;import java.util.concurrent.timeunit;/** * Check interrupt * @author Lenovo * */class Needscleanup {//Required To clear the 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 ensure proper cleanup n2try { SYSTEM.OUT.PRINTLN ("Compute Unit");//A time-consuming,non-blocking operation: Time-consuming, non-blocking operation for (int i = 1; i < 2500000; i++) {d = D + (Math.PI + MATH.E)/D;} System.out.println ("Complete time-consuming operation");} 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 Compute unit completes time-consuming operation cleaning up 2Cleaning up 1NeedsCleanup 1sleepingCleaning up 1exiting via inerruptedexecption*/




Java thread is blocked by mutex, check interrupt example explained----thinking JAVA4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.