Experience on Java multithreading programming

Source: Internet
Author: User
Tags constructor implement instance method variables thread thread class
Programming | Multithreading no matter how much I like Java however, from the actual development point of view, Java Multithreading does not have C + +.

Table Now:
1.Java no global variable;
2.Java communication between the threads is poor, C + + provides a variety of communication methods;
3.Java data synchronization is achieved through the synchronized, but basically equal to the virtual machine to complete, and C + + There are many kinds: critical areas, mutexes and so on.
4. Java's multithreaded Run method has no return value, so how to get feedback from a child thread is a real headache.
5.Java multithreading is collaborative, which is equal to the operating system to give up the control of the thread;

Here's how I write in Java Multithreading:

1. When creating thread, either the master class or the calling class is passed in to the constructor, for example: Class A calls class B,class A as an argument to the class B constructor. When you create a child thread, you implement it in the same way, so that the instance variables of the master class can be global variables, and of course, be aware of synchronization.

2. Class synchronization in Wait (), notify () must consider good logic, otherwise it may cause blocking.

3. If multiple threads call or are not currently aware of how many threads are communicating, the best approach is to implement a listener yourself, and then invoke the class to call an instance method of listener to communicate.

Working principle:

1) Listener interface provides synchronization methods such as Syndata ();
2 The synchronous thread provides the method of adding and removing listener, while the class of the registered listener is notified by rotation in a thread;
3 using the thread class to synchronize the data, inherit the listener interface, implement its method, and send the data of the end of this thread to the synchronization thread;

In fact, this principle comes from the Java swing technology.

Because of the time relationship, today is talking about here, hope to be able to talk!! Please give us a lot of advice!


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.