Interrupt () in Java, interruptexception and Wait (), sleep ()

Source: Internet
Author: User

Several concepts in the title are probably designed to be both thread synchronization and thread blocking. Thread synchronization, which is the same time, only one thread can execute the specified code; another thread block is that the current thread is temporarily parked in a location, waiting for a condition to be established before continuing.

Thread synchronization is to control the concurrency of multi-threaded work and cause the competition of shared resources. Java can be controlled by locking (monitor), in fact, is two keywords, one is synchronized, the other is lock, about the difference between the two, please Google yourself. The wait method must obtain the monitor of an object before it is allowed to execute, otherwise it will run out of monitor status exception. The wait () and join () methods are instance methods that are part of the object. The wait () method must be placed within the synchronization code block (as follows), which means that the wait () method of the object is allowed to execute after the monitor (lock) of the object is obtained, and after the Wait () method is executed, the current thread is blocked. At this time the current thread will release the monitor (lock) of the specified object when it comes in, while giving up the CPU, not competing in the CPU, waiting for other threads to execute the specified object's notify () or Notifyall () method to wake it up to continue execution (after being awakened, It is also necessary to get the lock of the specified object before it comes in, because the synchronization block must be in the monitor for the specified object before it comes in, and it is not from the new execution, after the monitor is obtained, it is executed from the previous wait (), since it was blocked here before. The CPU-related registers will remember where they were previously blocked).
 Public classService { Public voidTestMethod (Object lock) {Try {            synchronized(lock) {System.out.println ("Begin Wait () threadname=" +Thread.CurrentThread (). GetName ());                Lock.wait (); if(Thread.CurrentThread (). GetName (). Equals ("Thread-1") {thread.sleep (50000); } System.out.println ("End Wait () threadname=" +Thread.CurrentThread (). GetName ()); }        } Catch(interruptedexception e) {e.printstacktrace (); }    }     Public voidSynnotifymethod (Object lock) {synchronized(lock) {System.out.println ("Begin Notify () Threadname=" +Thread.CurrentThread (). GetName ());            Lock.notifyall (); System.out.println ("End Notify () Threadname=" +Thread.CurrentThread (). GetName ()); }    }}

As for the interrupt () method and the interruptexception exception, Java is specifically designed to handle thread blocking. A thread is blocked, which means to wait for a period of time. What if you need to wait a long time to interrupt a thread's blocking state before it ends? This is solved by the interrupt () method. If for some special reason, you want to interrupt some of the blocked threads prematurely so that they are unblocked in advance, and then continue to execute. You only need to invoke the interrupt () method of the specified thread in another thread (the interrupt () method is a thread instance method), when the corresponding thread that originally blocked throws a interruptexception exception. Catching an exception through a catch allows you to continue execution down the surface.    For example, the thread method sleep () and the instance method of object, wait (), will cause the current thread to block, and the interrupt () method can be used to exit the blocking state prematurely. Why can the interrupt () method interrupt The block prematurely? In fact, because each thread will have an interrupt status bit, it is called Interruptstatus Bar. The current thread sets the Interrruptstatus state bit to true to identify the current thread as blocked when these methods are currently executing sleep () and wait (). When the thread's interrupt () method is called, the Interruptstatus state is reset to false. The Sleep () and wait () methods are constantly polled to check the Interruptstatus state value, and if a moment becomes false, the current thread interrupts the blocking state and interrupts the blocking state by throwing interrupexception.    And then go on with the execution. As for the Wait () method and notify () the relationship between, through another article to tell.



From for notes (Wiz)



Interrupt () in Java, interruptexception and Wait (), sleep ()

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.