Java interrupt thread

Thread natural termination: natural or executing the throw unhandled exceptions

Java There are three ways to make a running thread terminates:

1. Exit sign that the thread exits normally;

2. Use the stop () method forcibly terminate the thread, but this method is not recommended, because the stop () and suspend (), resume (), the methods are obsolete, they are used to unpredictable results will occur; stop () It causes the thread will not release the resources correctly, suspend () easily lead to deadlock

3. Use the interrupt () method interrupt thread;

 

suspend () Deadlock example:

 public class SuspendTest1 {
     private static class SynchronizedObject {
         public synchronized void printString() {
             System.out.println("begin --");
             if (Thread.currentThread().getName().equals("thread-a")) {
                 System.out.println("a线程 suspend..");
                 Thread.currentThread().suspend();
             }
             System.out.println("end ---");
         }
     }
 ​
     public static void main(String[] args) {
         try {
             SynchronizedObject object = new SynchronizedObject();
             Thread thread1 = new Thread() {
                 @Override
                 public void run() {
                     System.out.println(Thread.currentThread().getName());
                     object.printString();
                 }
             };
 ​
             thread1.setName("thread-a");
             thread1.start();
 ​
             TimeUnit.SECONDS.sleep(1);
             Thread thread2 = new Thread() {
                 @Override
                 public void run() {
                     System.out.println("thread2 start...");
                     object.printString();
                     System.out.println("thread2 end...");
                 }
             };
             thread2.setName("thread-b");
             thread2.start();
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
 }

  

 

When thread2 run to thread2 start ... After consistent blocking view with jstack, as follows:

 "thread-b" #12 prio=5 os_prio=0 tid=0x000000001d169800 nid=0x8658 waiting for monitor entry [0x000000001ddcf000]
    java.lang.Thread.State: BLOCKED (on object monitor)
         at com.example.SuspendTest1$SynchronizedObject.printString(SuspendTest1.java:12)
         - waiting to lock <0x000000076b5a9568> (a com.example.SuspendTest1$SynchronizedObject)
         at com.example.SuspendTest1$2.run(SuspendTest1.java:40)
 ​
    Locked ownable synchronizers:
         - None

  


 

It found to occur BLOCKED, call object.printString in thread2 () Here deadlock;

This occurs because the internal operation println method, synchronization lock is not released;

 public void println(String x) {
     synchronized (this) {
         print(x);
         newLine();
     }
 }

  

 

Java thread is a collaborative, rather than preemptive

Non-preemptive: ongoing process during operation, if there are important or urgent process to reach (its status must be ready), the current running process will be forced to give up the processor, the system will assign the processor to immediately process the newly arrived; If you open task Manager to terminate a process;

Collaborative Java: Calling a thread's interrupt () method to interrupt a thread, not forced to close this thread, the thread will interrupt flag is true, the thread is interrupted, the decision by the thread itself (similar to greet people, but the person to return you do not go back is another matter); as follows:

A FutureTask, <String> = Future new new a FutureTask, <> (new new MyCallable ()); 
 the Thread the Thread = new new the Thread (Future); 
 Thread.start (); 
 Thread.interrupt (); 
 // If the asynchronous task is not yet completed, get () will block until the task is complete before returning the result 
 System.out.println (future.get ());

  

Here there are thread interrupts thread itself determined;

 

isInterrupted () determine whether the current thread into a break

static method interrupted () determine whether the current thread in the interrupt status and interrupt flag to false

If you throw in the method InterruptedException, thread interrupt flag will be reset to false, if indeed it needs to be interrupted thread, requiring calls interrupt ourselves once again in the catch block where ();

 

Guess you like

Origin www.cnblogs.com/coder-zyc/p/12468729.html