Java multithreading BASICS (4) traditional Java thread synchronous communication technology

Source: Internet
Author: User

Java multithreading BASICS (4) traditional Java thread synchronous communication technology
Java multithreading BASICS (4) Java traditional thread synchronous communication technology coding to implement the following functions

The sub-thread loops for 10 times, the main thread loops for 100 times, and then returns to the sub-thread loop for 10 times, and then returns to the main thread for another 100 times, so that the loop is 50 times.

Analysis

1) The subthread must be mutually exclusive for 10 cycles and 100 cycles of the main thread, and there cannot be cross-execution. The following code uses the synchronized keyword to implement this requirement;
2) The subthread and the main thread must alternate. This can be achieved through synchronous thread communication technology. The following code uses the bShouldSub variable to implement this requirement;

Other notes

1) The business variable must be declared as the final type, because the local variables called in the anonymous internal class and local internal class must be final, so as to ensure:
-Consistency of variables (final variables will be copied as local internal class variables during compilation );
-Avoid inconsistency between the lifecycle of local variables and objects of local internal classes.

Otherwise,
-If the local variable is modified in the external class after it is passed into the local internal class, the value of this variable in the internal class is inconsistent with that in the external class, unpredictable situations may occur;
-Otherwise, the life cycle of local variables is cleared from the stack as the method ends, and a local internal class accesses a nonexistent variable.
If it is a member variable, it does not need to be final.

For details, refer to the following articles:
Http://feiyeguohai.iteye.com/blog/1500108
Http://blog.csdn.net/whuslei/article/details/6251020
2) The internal category can be divided into four types: member Internal category, static internal category, local internal category, and anonymous internal category. For the life cycle and detailed usage of the four types, please ask Google or du Niang.

Code Implementation
Package cn. king; public class TraditionalThreadCommunication {public static void main (String [] args) {// must be declared as final Business business = new Business (); new Thread (new Runnable () {@ Override public void run () {for (int I = 1; I <= 50; I ++) {business. sub (I );}}}). start (); for (int I = 1; I <= 50; I ++) {business. main (I) ;}} class Business {// this variable is used for inter-thread communication private boolean bShouldSub = true; public Synchronized void sub (int I) {if (! BShouldSub) {try {this. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}}for (int j = 1; j <= 10; j ++) {System. out. println (sub thread sequence of + j +, loop of + I);} bShouldSub = false; this. notify ();} public synchronized void main (int I) {if (bShouldSub) {try {this. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}}for (int j = 1; j <= 100; j ++) {System. out. println (main thread sequence of + j +, loop of + I);} bShouldSub = true; this. notify ();}}

 

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.