Multi-threaded interview shared data resolution:
A. What is multithreading
? threads are a single sequential control flow in a program. Running multiple threads at the same time in a single program completes different tasks, called multithreading.
All of the threads are running serially in the micro, but on the macro you can feel that they are running in parallel
two. Multi-threaded interview shared data resolution
1, assuming that each thread runs the same code that can use the same Runnable object, the Runnable object has that shared data.
For example, the ticketing system.
2 runnable three way runnable data sharing between objects:
runnable objects Span lang= "ZH-CN" xml:lang= "ZH-CN". Each thread also assigns the method of the shared data to the object to complete, so that the easy implementation of the data for each operation of the mutual exclusion and communication.
?? put these Runnable object as an inner class in a class , shared data as a member variable in this external class . Each thread's method of manipulating shared data is also assigned to an external class to enable the mutual exclusion and communication of various operations on the shared data. These methods for external classes are called by each Runable object that is an inner class.
???? The combination of the above two methods : Encapsulates the shared data in another object, and each thread assigns the method of the shared data to that object as well, and the object acts as a member variable in the outer class or as a local variable in the method. Each thread of the Runnable object acts as a member of an inner class or a local inner class in an outer class.
?? In short, it is best to synchronize several pieces of code that are mutually exclusive, preferably in a few separate methods, which are then placed in the same class, so that the synchronization between them is more easy to repel and communicate with each other.
?
an external Class a There are two inner classes, these two inner classes Span lang= "en-us" xml:lang= "en-US" >b , c How do I share data?
-- B , C both manipulate external classes A the same member variable. So. Two Runnable objects to share the same data, you can use two Runnable as the inner class of the outer class, and share the data as the member variable of the external class.
three. Examples:
1 , assuming that each thread runs the same code. Ability to use the same Runnable object. This Runnable object has that shared data. For example, the ticketing system.
Code Explanation: Open three threads and access shared data count at the same time. are performed on them--operations. Implementation idea: Because three threads run the same code (for shared data operations as well). So using the same runnable (for example, the following code: Class ShareData1 implements the Runnable interface), the Runnable object encapsulates the shared data count. That is, the operation on the shared data count.
Package com.tgb.thread07;/** * Multi-threaded access to shared objects and data--mock ticketing system * scenario: Because each thread runs the same code (all on the existing number of votes) when buying a ticket. So you can use the same Runnable object, * This Runnable object has that shared data. For example, the ticketing system. * * @author hanxuemin * @date July 30, 2015 10:06:35 * */public class Multithreadsharedata {public static void main (String [] args) {ShareData1 data1 = new ShareData1 ();/** * Open three threads, sell tickets, access shared data */new thread (data1). Start (); new Thread (DATA1). Start ( ); new Thread (Data1). Start ();}} /** * Includes Runnable implementation class for shared data ShareData1 * @author hanxuemin * */class ShareData1 implements Runnable {private int cou NT = 100; Total 100 tickets (shared data) public void run () {while (Count > 0) {count--;//Votes -1system.out.println (Thread.CurrentThread (). GetName () + "after the ticket. The total number of votes is: "+ count";}}}
2 。 Assuming that each thread is running different code, this time requires a different runnable three way runnable data sharing between objects:
? ? Way One: encapsulating shared data in another object , and then This object is passed to each Runnable Object . Each thread's method of manipulating shared data is also assigned to that object to complete. This easy implementation of the data for the various operations of mutual exclusion and communication.
Demo: Design 4 threads, with two threads adding 1 to J each time, and two threads reducing the J by 1 per second. Write the program.
Code Explanation: (1) The shared data is J, two threads to it + +, two threads to it--so the code runs differently, so different runnable objects are required, (2) read that 4 threads have two operations on shared data, so create two different runnable implementation classes-- MyRunnable1, MyRunnable2; (3) in creating a class ShareData2, encapsulate the shared data in that class. At the same time, each thread is encapsulated to manipulate the shared data, and (4) each ShareData2 object is passed to each Runnable object one after the other.
Package com.tgb.thread07;/** * Multi-threaded access to shared objects and data-the code that runs on each thread is different * scenario: assuming that each thread runs different code, this time requires a different runnable object, There are three ways to implement data sharing between these runnable objects, such as: * Scenario One: Encapsulate the shared data in another object, and then pass the object to each Runnable object individually. * Each thread's method of manipulating shared data is also assigned to that object to complete. This easy implementation of the data for the various operations of mutual exclusion and communication. * * This program implementation demo: Design 4 threads, of which two threads each add 1 to J. The other two threads reduce the J by 1 at a time. Write the program. * Analysis: (1) There are two operations for shared data:<a> for shared data J ++;<b> for shared data J-; --so * need to create two Runnable objects (j + + and--) * * @author hanxuemin * @date July 30, 2015 10:06:35 * */public class Multithreadshare DATA02 {public static void main (string[] args) {final ShareData2 data2 = new ShareData2 ();//Creates a ShareData2 instance object that encapsulates shared data. And how each thread operates on shared data) for (int i = 0; i < 2; i++) {/** * creates two runnable objects. The Data2 object that encapsulates the shared data is passed to these two Runnable objects one at a */myrunnable1 myRunnable1 = new MyRunnable1 (DATA2); MyRunnable2 myRunnable2 = new MyRunnable2 (DATA2);/** * starts two threads, which two threads use two different runnable */new thread (myRunnable1). Start (); New Thread (MyRunnable2). Start ();}}} /** * MYRUNNABLE1 Implementation Runnable Interface * * @author hanxuemin * */class MyRunnable1 implements Runnable {private ShareData2 data2;p Ublic MyRunnable1 (ShareData2 data2) {this.data2 = data2;} @Overridepublic void Run () {data2.increment ();//Call + + method}}/** * MyRunnable2 class implements runnable interface * * @author hanxuemin * */class MyRunnable2 implements Runnable {private ShareData2 data2;public MyRunnable2 (SharedaTa2 data2) {this.data2 = data2;} @Overridepublic void Run () {data2.decrement ();//Invoke--method}}/** * Encapsulates the class SHAREDATA2 for shared data. At the same time, the class encapsulates how each thread operates on shared data * * @author hanxuemin * */class ShareData2 {private int j = 100;//Shared Data j/** * method for J + + */pub Lic synchronized void Increment () {j + +; System.out.println ("+ + method:" + Thread.CurrentThread (). GetName () + "operation after sharing data. The value of the current shared data is "+ J";} /** * for J-method */public synchronized void decrement () {j--; System.out.println ("--method" + Thread.CurrentThread (). GetName () + "Operation shared data, the value of the current shared data is" + j);}}
Four, summary
The difference is that the situation uses a different approach.
Two other ways, see next blog post
Multi-threaded interview shared data (1)