How shared data is handled between multiple threads in Java

Source: Internet
Author: User

Title Requirements:


There are 4 threads, of which two threads each X plus 1, the other two X minus 1 each time, how to implement?

Analysis:
X is the 4 threads to process the shared data, different kinds of threads have different processing methods, but the operation of the data is common, Lenovo to "window ticket problem", but the ticket sales are simply minus.

So, the way multiple threads access shared objects or data, look first, if each thread executes the same code , then you can use the same Runnable object, which has shared data, such as ticket purchase problems.

There are two ways to implement data sharing between these different runnable objects if the code executed by each thread differs from the Runnable object that you do not need:

The first is to encapsulate the shared data into an object, pass the object that the shared data is on to a different runnable, and share the data with each thread.

object is completed so that mutual exclusion and communication can be achieved for the data processing.

Class Sharedata {private int x = 0;public synchronized void Addx () {x + +; System.out.println ("x + +:" +x);} Public synchronized void Subx () {x--; System.out.println ("x--:" +x);}}  Class MyRunnable1 implements Runnable{private sharedata share1 = Null;public MyRunnable1 (sharedata share1) {this.share1 = Share1;} public void Run () {for (int i = 0;i<100;i++) {SHARE1.ADDX ();}}}  Class MyRunnable2 implements Runnable{private sharedata Share2 = Null;public MyRunnable2 (sharedata share2) {This.share2 = Share2;} public void Run () {for (int i = 0;i<100;i++) {SHARE2.SUBX ();}}} public class Threadsvisitdata {public  static void Main (string[] args) {sharedata share = new Sharedata (); New Thread (n EW MyRunnable1 (Share)). Start (); New Thread (New MyRunnable2 (Share)). Start ();}}


The second type:

These runnable objects are used as inner classes of a class, shared data as member variables of an external class, and methods for sharing data to be assigned to external classes to accomplish the sharing of operations
Data mutual exclusion and communication, as an internal class of runnable to manipulate the method of the external class, to achieve the operation of the data


Class Sharedata {private int x = 0;public synchronized void Addx () {x + +; System.out.println ("x + +:" +x);} Public synchronized void Subx () {x--; System.out.println ("x--:" +x);}} public class Threadsvisitdata {public static sharedata share = new Sharedata (), public static void main (string[] args) {/ /final sharedata share = new Sharedata (); New Thread (New Runnable () {public void run () {for (int i = 0;i<100;i++) {Share.a DDX ();}}). Start (); New Thread (New Runnable () {public void run () {for (int i = 0;i<100;i++) {SHARE.SUBX ()}}). Start (); }}

Summary: To synchronize mutually exclusive code tasks it is best to put them separately in separate methods, these methods are placed in the same class, it is easier to implement the operation of synchronization and communication


How shared data is handled between multiple threads in Java

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.