How multiple threads access shared objects and data

Source: Internet
Author: User

There are two scenarios in which multiple threads access shared objects and data:
1, each thread executes the same code, for example, sell tickets: Multiple windows simultaneously sell the 100 tickets, these 100 tickets need multiple threads to share.
2. Each thread executes a different code, for example: Design four threads, where two threads add 1 to J each time, and two threads decrease by 1 per second.

A, if each thread executes the same code, you can use the same Runnable object, which has shared data. Sell tickets can do this, each window is to do the ticket sales tasks, sell tickets are the same data (click to view the specific case).

b, if each thread executes different code, you need to use different runnable objects, there are two ways to implement data sharing between the Runnable objects:
1), separate the shared data into an object, and provide the method of manipulating these shared data in the object, which can make the mutual exclusion and communication of the operation of the shared data easy.
2,) each Runnable object as an inner class of a class, shared data as a member variable of the external class, the operation method of the shared data is also provided in the external class, in order to implement mutual exclusion and communication, the inner class of the Runnable object calls the external class to manipulate the method of sharing data.

Package com.ljq.test.thread;/** * How data is shared between multiple threads * * Design four threads, where two threads add 1 to J each time, and two threads decrease by 1 for J at a time. Cycle 100 times. * * @author Administrator * */public class Multithreadsharedata {private static Sharedata data1 = new Sharedata ();p ublic static void Main (string[] args) {sharedata data2 = new Sharedata (); New Thread (New Decrementrunnable (DATA2)). Start (); New T Hread (New incrementrunnable (DATA2)). Start (), final sharedata data1 = new Sharedata (); New Thread (New Runnable () {@ overridepublic void Run () {data1.decrement ();}}). Start (); New Thread (New Runnable () {@Overridepublic void run () {data1.increment ();}}). Start ();}} /** * Create thread class, responsible for J Reduction 1 * * @author Administrator * */class Decrementrunnable implements Runnable {private Sharedata data;pub Lic decrementrunnable (sharedata data) {this.data = data;} public void Run () {for (int i=0; i<100; i++) {data.decrement ();}}} /** * Create thread class, responsible for J add 1 * * @author Administrator * */class Incrementrunnable implements Runnable {private Sharedata data;pub Lic incrementrunnable (sharedata data) {this.data = data;} public void Run () {for (int i=0; i<100; i++) {data.increment ();}}} /** * Package Shared data * * @author Administrator * */class sharedata {private int j = 0;/** * Add 1 to J each time */public synchronized void in Crement () {j + +; System.out.println ("j++=" +j); /** * 1 */public per J reduction synchronized void decrement () {j--; System.out.println ("j--=" +j);}

Note: Several pieces of code to synchronize mutexes are best placed in separate methods, which are placed in the same class, which makes it easier to synchronize mutual exclusion and communication between them.

How multiple threads access shared objects and data

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.