Thread advanced application-experience 7-JAVA5 application and case analysis of blocking queue condition in thread concurrent library

Source: Internet
Author: User

1. Blocking Queue Knowledge points

Blocking queues It is important to use the following methods, which can be used to refer to the Help document; It is clear that the first method does not block the direct throw exception; The second method is Boolean, the blocking returns Flase, and the third method blocks directly.

2. Case study One: Package Com.java5.thread.newSkill;
Import Java.util.concurrent.ArrayBlockingQueue;

Import Java.util.concurrent.BlockingQueue; public class Blockingqueuetest {/** * Blocking Queue class: Blockingqueue/public static void Main (string[) args) {final B
		
		Lockingqueue queue = new Arrayblockingqueue (3); for (int i=0;i<2;i++) {new Thread () {public void run () {while (true) {try{thread.sleep (long) Mat
						H.random () *10000); System.out.println (Thread.CurrentThread (). GetName () + "ready to put the data."
						");
						Queue.put (1); System.out.println (Thread.CurrentThread (). GetName () + "has already put the data, the queue currently has:" +queue.size () + "data.
					");
					}catch (Exception e) {e.printstacktrace ();
		}}}.start ();    New Thread () {public void run () {while (true) {try{//change the sleep time here to 100 and 1000 respectively to observe the run result * * Short rest time, the theory should be: The data will never reach three, always be taken away in the second; * But my operation always has three data appear, do not know the reason is, * Interested Daniel can copy code test; * Long rest time, the data is always three
				, but put more to take less * *	Thread.Sleep (100); System.out.println (Thread.CurrentThread (). GetName () + "prepares to fetch data."
					");
					Queue.take (); System.out.println (Thread.CurrentThread (). GetName () + "has taken the data, the queue currently has:" +queue.size () + "data.
				");
				}catch (Exception e) {e.printstacktrace ();
	}}}.start ();

} II: Package Com.java5.thread.newSkill;
Import Java.util.concurrent.ArrayBlockingQueue;

Import Java.util.concurrent.BlockingQueue;  /** * */public class Blockingqueuecommunication {public static void main (string[] args) {final Business Business
		= new Business (); New Thread (New Runnable () {@Override public void run () {for (int i = 1; I <= i++) {business.sub (
				i);

		}}). Start ();
		for (int i = 1; I <= i++) {business.main (i); }/* * Write a business class with a child method (to invoke the child thread) and the main method (invoke the main thread) to add static because the new object above is final. In order to make this class an external class, * But there is a class with the same name outside, so do this; the resulting class name is: Blockin Gqueuecommunication.bussiness */Static class Business {blockingqueue<integer> queue1 = new arrayblockingqueue<integer> (1);

		blockingqueue<integer> queue2 = new arrayblockingqueue<integer> (1);
		 * * This code, which is written directly in curly braces, is called the anonymous construction method, and the anonymous construction method takes precedence over any other construction method.
		 * With the static keyword is called a code block, not with the common code block * Static code blocks are executed at the time the class is loaded, only once, and the normal code block creates several objects that are executed several times.
				* * {try {System.out.println ("I did, put a data in the Queue2");
			Queue2.put (1);
			catch (Interruptedexception e) {e.printstacktrace ();
				}//Here to be aware of the problem: must not use the synchronous lock sychronized, otherwise the effect can not reach the rough//Because the blocking is already similar to the function of synchronization, and then use the synchronous lock is dead lock public void sub (int i) {try {
			Queue1.put (1);
			catch (Interruptedexception e) {e.printstacktrace ();
			for (int j = 1; J <= J + +) {System.out.println ("Sub thread sequence of" + j + ", loop of" + i);
			try {queue2.take ();
			catch (Interruptedexception e) {e.printstacktrace ();
			} public void Main (int i) {try {queue2.put (1);
			catch (Interruptedexception e) {e.printstacktrace (); for (int j = 1; J <= 100;
			J + +) {System.out.println ("main thread sequence of" + j + ", loop of" + i);
			try {queue1.take ();
			catch (Interruptedexception e) {e.printstacktrace ();
 }
		}
	}
}


 

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.