Pipeline communication (pipe) and common exception handling in Java multi-threaded communication __java

Source: Internet
Author: User
Tags exception handling static class

Please respect the fruits of labor, reproduced please specify the source: http://blog.csdn.net/caoshichao520326/article/details/8995583

Java Multithreading to exchange information, sometimes only pipe to complete, in the use of pipe communication, often encounter "Java-ioexception:read end Dead" or "Java-ioexception:write-end dead" anomaly, The following for this problem to make a simple analysis, the first is to write a pipeline communication demo for everyone's reference. The procedure is as follows:

Package com.csc.pipetest;
Import java.io.IOException;
Import Java.io.PipedInputStream;

Import Java.io.PipedOutputStream; /** * Pipeline Communication Demo * * @author CSC * */public class Pipetest {public static void main (string[] args) {//Create pipeline output
		Stream PipedOutputStream pos = new PipedOutputStream ();
		Create a pipe input stream pipedinputstream pis = new PipedInputStream ();
		try {//stream the pipeline inflow and output the process can also be implemented by overloaded constructors Pos.connect (PiS);
		catch (IOException e) {e.printstacktrace ();
		//Create producer thread Producer p = new Pipetest (). New Producer (POS);
		Create consumer thread Consumer c = new Consumer (PiS);
		Start thread P.start ();
	C.start ();

		//Producer threads (associated with a pipeline input) private class Producer extends thread {private PipedOutputStream pos;
		Public Producer (PipedOutputStream pos) {this.pos = pos;
			public void Run () {int i = 8;
			while (true) {//Join this sentence will appear "Java-ioexception:read end Dead" Exception try {pos.write (i);
			catch (IOException e) {e.printstacktrace (); }
			// }

		}
	}

	Consumer threads (associated with a pipeline input) private static class Consumer extends thread {private PipedInputStream pis;
		Public Consumer (PipedInputStream pis) {this.pis = PiS; } public void Run () {//while (true) {//Join this sentence will appear "Java-ioexception:write end Dead" Exception try {System.out.println
			(Pis.read ());
			catch (IOException e) {e.printstacktrace ();
 }
			// }

		}
	}
}

The above program can be run correctly, ① will throw "Java-ioexception:read End Dead" If you add code that is commented out on lines 46th and 52nd. ② "Java-ioexception:write End Dead" will be thrown if you add code that is commented out on lines 66th and 72nd. The reason is the same: when using a pipe to read and write data, you must ensure that the thread that uses the pipe to read and write data cannot exit. For the above program, if the ① is the case, because the consumer (consumer) thread after reading the data in the pipeline, the thread runs to the end of the exit. The exception is thrown when the data is written to the thread producer that established the link pipe. Similarly, if the ② is the case, because the producer (producer) thread writes the data in the pipeline, the thread runs to the end of the exit. The exception is thrown when the data is read from the thread consumer that established the link pipe.

The attentive reader will find that the 37th and 58th lines of the program create an inner class that uses a "static" keyword, one that is not used, which determines how the program's 28th and 30th rows differ in the way that the object is instantiated, which is required to create an internal class, or a compilation error occurs. Detailed analysis See: http://blog.csdn.net/caoshichao520326/article/details/8961297

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.