JAVA concurrency-Interrupt processing and task cancellation __java

Source: Internet
Author: User
Interrupt processing
In Java programs, you need to handle interruptedexception when you use a blocking method such as Thread.Sleep () or Blockingqueue.take (). For this exception, there are usually 2 scenarios for processing.
1. Passing Exceptions: Passing exceptions to the caller of the method. Examples are as follows:
blockingqueue<string> queue;
Public String getnextstring () throws interruptedexception{return
		queue.take ();
} 

2. Restore exceptions: In most cases, exceptions can be passed, but in some cases it is not possible to pass exceptions, such as in the Runnable run () method, where we cannot throw exceptions. At this point we need to restore the exception so that the higher-level code in the call stack will see a break. Examples are as follows:
blockingqueue<string> queue;
public string getnextstring () throws interruptedexception{
		string result=null;
     try{
			Result=queue.take ();
} catch (Interruptedexception e) {
	Thead.currentThread.interrupt ();
} finally{return result
	;
}
 

The above is the common method of handling interruptedexception, for interruptedexception, do not catch exception but do not do any processing. When we end a thread with interrupts, we can also use interrupted () in the catch block to clear the exception.

Task canceled
When multithreaded programming, sometimes you need to cancel some of the task threads, there are the following 3 scenarios:
1. Set a thread cancellation token, the task thread periodically check this tag. Examples are as follows:
Class Task implements runnable{
	private volatile Boolean cancel=false;
	@Override public
	Void Run () {while
		(!cancel) {
			System.out.println ("...");
		}
	}
	public void Cancel () {
		cancel=true
	}}

2. The above example describes the most general scenario, and imagine that if a blocking method is invoked in the while (!cancel) loop, there is a possibility that the program might block in a method. Examples are as follows:
Class Task implements runnable{
	private volatile Boolean cancel=false;
	Private blockingqueue<string> Blockingqueue;
	Public Task (blockingqueue<string> queue) {
		This.blockingqueue=queue
	}
	@Override public
	Void Run () {
		try{while
			(!cancel) {
				System.out.println ("...");
				This.blockingQueue.take ()///When the program is blocked here, even if Cancel is updated and cannot be sensed, the program will never exit.
			}			
		} catch (Interruptedexception e) {
			thread.currentthread (). interrupt ();
		}
	}
	public void Cancel () {
		cancel=true
	}}


When a blocking method is invoked in a while (!cancel) loop, terminating the program using a marker bit is no longer in use, and exiting the program in the form of a break:
Import Java.lang.reflect.Method;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.concurrent.ArrayBlockingQueue;

Import Java.util.concurrent.BlockingQueue; public class CDemo {public static void main (String [] args) throws exception{Blockingqueue<string&gt ;
        	B=new arrayblockingqueue<string> (3);
        	B.put ("a");
        	B.put ("AB");
        	
        	B.put ("abc");
        	Task Task=new task (b);
        	Task.start ();
        	
        	Thread.Sleep (4000);
        Task.cancel ();  
    } class Task extends thread{private blockingqueue<string> blockingqueue;  
    Public Task (blockingqueue<string> queue) {this.blockingqueue=queue; @Override public void Run () {try{while (true) {//if (Thread.CurrentThread (). I
                Sinterrupted ())//must be aware that this line is an error procedure if (interrupted ())//To determine whether the current thread is interrupted; 
     String Str=this.blockingqueue.take ();           System.out.println (str);  
        	}}catch (Interruptedexception e) {//thread.currentthread (). interrupt (); Interrupted ()//Clear Interrupt traces}finally{System.out.println ("Thread End".
        "); } public void Cancel () {this.interrupt ();//interrupt current thread//Thread.CurrentThread (). interrupt ();  

 This line is wrong procedure}}

3. In the issue of producer consumers, the use of "poison pill object" to terminate the consumer thread.
Import Java.util.concurrent.ArrayBlockingQueue;
Import Java.util.concurrent.BlockingQueue;

Import Java.util.concurrent.CountDownLatch; public class Coordinator {public static final Object Poison_pill = new Object ();//special Object to kill consumers Priv
	ate int productcount = 1;

	private int consumercount = 3;
		public void Startall () throws exception{blockingqueue<object> queue = new arrayblockingqueue<object> (5);
		Countdownlatch activeproductornum = new Countdownlatch (productcount);

		Countdownlatch activeconsumernum = new Countdownlatch (consumercount);
		for (int i = 0; i < Consumercount i++) {new Thread (new Consumer ("Consumer" + I, Queue,activeconsumernum)). Start (); for (int i = 0; i < ProductCount i++) {new Thread (new Producer ("Producer" + I, queue, Activeproductornum)). s
		Tart (); Activeproductornum.await ()//waiting for all producer production to end System.out.println ("All producer finished, putting Poison_pill to the Qu
		Eue to stop consumers! "); Queue.put (POison_pill);
	Activeconsumernum.await ()//waiting for all producers to produce an end System.out.println ("All Consumer finished!");
	public static void Main (string[] args) throws exception{new Coordinator (). Startall ();
	} class Producer implements Runnable {private String name;
	Private blockingqueue<object> queue;
	
	Private Countdownlatch Activeproducernum;
		Public Producer (String name, blockingqueue<object> queue, Countdownlatch activeproducernum) {this.name = name;
		This.queue = queue;
	This.activeproducernum=activeproducernum;
				@Override public void Run () {try {A-for (int i=0;i<10;i++) {queue.put (i);				
			SYSTEM.OUT.PRINTLN (name + "produced" +i);
		} catch (Interruptedexception e) {thread.currentthread (). interrupt ();
			} finally{System.out.println (name + "finished.");
		Activeproducernum.countdown ();
	Class Consumer implements Runnable {private String name;
	Private blockingqueue<object> queue; Private Countdownlatch ACTiveconsumernum;
		Public Consumer (String name, blockingqueue<object> queue,countdownlatch activeconsumernum) {this.name = name;
		This.queue = queue;
	This.activeconsumernum=activeconsumernum;
				@Override public void Run () {a try {while (true) {The Object item = Queue.take ();
				if (item = = Coordinator.poison_pill) {queue.put (item);//put back to continue to poison other consumers break;
			} System.out.println (name + "consumed" +item);
		} catch (Interruptedexception e) {thread.currentthread (). interrupt ();
			} finally{System.out.println (name + "finished");
		Activeconsumernum.countdown (); }
	}
}






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.