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 variables;
2.Java communication between the threads is poor, C + + provides a variety of communication methods;
3.Java of data synchronization is achieved through synchronized, but basically it is given to the virtual machine to complete,
There are many kinds of C + +: Critical section, mutex, and so on.
4. The multithreaded Run method in Java does not return a value, so how to get feedback from a child thread is really a headache.
5.Java multithreading is collaborative, so that the operating system has abandoned the control of the thread;
Here's how I write in Java Multithreading:
1. When you create a thread, pass the master class or call class into the constructor, for example:
Class A calls class B,class A as a parameter to the class B constructor.
This is done in the same way when a child thread is created, so that the instance variable of the master class can be used as the
Global variables, of course, be aware of synchronization.
2. Wait () in class synchronization, notify () must take good logic into account, or it may cause blocking.
3. If multiple threads are called or are not currently clear how many threads are communicating, the best way is
Implement a listener yourself, and then call the class to call an instance method of listener to communicate.
Working principle:
1) Listener interface provides synchronization methods such as Syndata ();
2) The synchronization thread provides methods for adding and removing listener, while in-line registration listener
class to be notified in turn;
3) Use the thread class to synchronize the data, inherit the listener interface, implement its method, send the data of the end of this thread to the synchronization thread;
Java multithreaded programming