How to process shared data between multiple Java threads

Source: Internet
Author: User

How to process shared data between multiple Java threads
Question requirements:


There are four threads, two of which add 1 to x each time, and the other two subtract 1 from x each time. How can this problem be achieved?

Analysis:
X is the shared data to be processed by these four threads. Different threads have different processing methods, but the data to be operated is the same. It is thought of as "window ticket purchasing problem ", however, the ticket is sold only by a simple reduction.

Therefore, when multiple threads access shared objects or data, first look at it. If the code executed by each thread is the same, you can use the same Runnable object, which contains shared data, if you have a ticket.

If the code executed by each thread is different, Runnable objects are definitely not needed. There are two ways to achieve data sharing between these different Runnable objects:

First, encapsulate the shared data into an object, and pass the object where the shared data is located to different Runnable. Each thread's operations on the shared data are also assigned to that

Object To achieve mutual exclusion and communication of 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(new MyRunnable1(share)).start();new Thread(new MyRunnable2(share)).start();}}


Second:

The Runnable object is used as the internal class of a class, the shared data is used as the member variable of the external class, and the operations on the shared data are assigned to the external class method, share operations
Data mutex and Communication, as the internal class Runnable to operate the external class method, to achieve data operations


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.addx();}}}).start();new Thread(new Runnable() {public void run() {for(int i = 0;i<100;i++){share.subx();}}}).start(); }}

Conclusion: It is best to place mutually exclusive code tasks in independent methods and put these methods in the same class, which makes it easier to synchronize and communicate operations.


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.