1. Pack the shared data in another object, and then pass this object to each Runnable object one by one. Each thread’s operation method of shared data is also allocated on that object to complete, so that it is easy to realize the mutual exclusion and communication of each operation of the data.
2. Use these Runnable objects as internal classes in a certain class, sharing data as member variables in this external class, and each thread is also assigned to the external class to the shared data to achieve the shared data. The mutual exclusion and communications of each operation are used as the method of calling the external class as each Runnable object in the internal class.
The combination of the above two methods: encapsulate the shared data in another object, and the operation method of the shared data on each thread is also assigned to the object. Local variables, the Runnable object of each thread is the internal or part of the internal class of members in the external class.
In short, it is best to simultaneously placed in several independent methods. These methods are placed in the same class, which is easier to achieve synchronization and communication between them.