Home » Java » Java with two

Java with two

1, thread interrupts:

interrupt thread is an important mechanism of collaboration. Thread thread interrupts will not make the thread exit, just send a notification to the thread, the thread execution process will detect this notice, then you can customize the action, such as exit, but not all exit.

There are three ways to break in a


public void interrupt () interrupts threads
The public booklean isInterrupted () determines whether the interrupt is not clear and the current interrupt state is not cleared
The public static Boolean interrupt () determines whether it is interrupted and clears the current interrupt state,.

public, class, ThreadInterrupt {
Public, static, void, main (String, args[]), throws, InterruptedException {
Thread T1 = new, Thread () {
Public, void, run () {
While (true) {
If (Thread.currentThread (),.IsInterrupted ()) {//1 does not exit in the interrupt process
2 / / this method is not clear the interrupt flag, and therefore will be detected
System.out.println (thread first detected interruption, not exit);
System.out.println (thread execution);
If (Thread.currentThread (),.IsInterrupted ()) {
System.out.println (thread again detects interrupts and then exits);
System.out.println (thread execution);
Thread.yield ();
T1.start ();
Thread.sleep (3000);
T1.interrupt ();


Thead.sleep) method will make the current thread dormant for some time, will throw a InterruptedException exception interrupt. Not a runtime exception, the program must capture it. When the thread in sleep sleep, if it is interrupted, the exception will produce.


Thread.sleep method due to the interruption and throw an exception, at this time, it will clear the interrupt flag, if not treated, then in the next cycle begins, it is unable to capture the interrupt, the exception handling, once again set the interrupt flag bit.

2, wait for the wait notification notify

in order to support collaboration between multiple threads, JDK provides two important interface: wait and notify methods. The two methods defined in the Object class, which means that any object can call the two methods, but wait and notify are not free to call, it must be included in the corresponding synchronized statement in another place needs to pay attention, try not to call on an instance of Thread wait and notify. Either wait or notify, you need to first obtain a.

object monitor

thread executes the wait and notify processes:

T1 first gets the monitor of the object object before executing the object.wait method, and then releases the object monitor when the thread enters the waiting state and waits for the wakeup.

of the other threads

T2 in notify before the call, the monitor must be object, then T1 has released the monitor, the monitor so T2 can successfully obtain a object T2 implementation of the notify method. Then try to wake up a waiting thread here is random to wake up, wake up the T1 hypothesis.T1 wakes up, the first thing you do is the following code is not, but try to get the new object from the monitor, the monitor is T1 in the.

held before the implementation of the wait method

object.wait and thread.sleep methods can make the thread is waiting for some time. In addition to wait can be aroused, another major difference is that wait will release the target lock, while the Thread.sleep method does not release any resources.

3, hang suspend to continue resume
In the

cause the thread to pause at the same time, and will not release any resources. At any other thread to access it by temporary lock, will be implicated. Lead to normal operation. Continue until resume, assuming no resume execution, or resume has been called in advance, then the thread will be suspended.

4, wait for the thread to end join, humility yield

join has two method signatures, with no arguments for join, indicating infinite wait, and parameter join indicating waiting for the specified time.

calls T2.join in the thread T1 to wait until the T2 thread is called, and then calls the thread T1.

(obviously, calling your own join methods is meaningless in your own way.)

yield is a static method, once implemented, it will make the current thread for CPU, but it should be noted that, for CPU does not mean that the current thread is not executed in the current thread. For CPU, for.

also CPU resources
5, daemon thread

Daemon is a kind of special thread, it is the guardian, to complete some systematic service silently in the background. For example, garbage collection thread and JIT thread. When a Java application, only the daemon thread, Java virtual machine will exit the.

6, about the use of synchronized:

specifies the lock object that locks the given object and gets the lock of the given object before entering the synchronization code.

Direct acting on instance methods: equivalent to locking the current instance and getting the lock of the current instance before entering the synchronization code.

Direct acting on the static method: equivalent to locking the current class; to get the lock.

of the current class before entering the synchronization code