Android multithreaded Research (5)--sharing data between threads

Source: Internet
Author: User

First, if each thread executes the same code, the same runnable can be used for sharing

public class Multithreadsharedata {public static void main (string[] args) {new Thread (new Sharedata ()). Start (); new thread (New Sharedata ()). Start ();} Static class Sharedata implements Runnable{private int j = @Overridepublic synchronized void run () {j--;}}}
The two threads in the above code share the data to implement the decrement of the J variable, as to why a static inner class is used in the above code, the function of which is to not rely on an instance of an external class to create an object.

Second, if each thread executes a different code, you need to use a different runnable object for sharing

public class Multithreadsharedata {public static void main (string[] args) {final Sharedata data1 = new Sharedata ();//Start First Thread new Thread (new Runnable () {@Overridepublic void run () {data1.increment ();//Plus}}). Start ();//starts the second thread, new Runnable () {@Overridepublic void run () {data1.decrement ();  Minus}). Start ();} Static class Sharedata{private Int j = 0;public synchronized void increment () {j + +;} Public synchronized void Decrement () {j--;}}}

Modify the above code (use the data as a member variable of the external class, let the runnable interface manipulate the member variable) as follows:

public class Multithreadsharedata {public static void main (string[] args) {Sharedata data1 = new Sharedata (); New Thread (NE W MyRunnable1 (Data1)). Start (); New Thread (New MyRunnable2 (DATA1)). Start (); Static class MyRunnable1 implements Runnable{private sharedata data1;public MyRunnable1 (sharedata data1) {this.data1 = Data1;} @Overridepublic void Run () {data1.increment ();}} Static class MyRunnable2 implements Runnable{private sharedata data1;public MyRunnable2 (sharedata data2) {this.data1 = Data1;} @Overridepublic void Run () {data1.decrement ();}} Static class Sharedata{private Int j = 0;public synchronized void increment () {j + +;} Public synchronized void Decrement () {j--;}}}

Three or one-face question (design 4 threads, where two threads add 1 to J each time, and two threads to j each reduce by 1 per second)

/** * Design 4 (can be n) threads, of which two threads each add 1 to J, and two threads to J each reduce 1 */package com.jiaocaigen.test;       public class Test {//multiple threads created with the Runnable interface can share instance properties private int i;        Sync Add method private synchronized void Inc () {i + +; System.    Out. println (Thread.CurrentThread (). GetName () + "--inc--" + i);        }//Synchronous subtraction method private synchronized void Dec () {i--; System.    Out. println (Thread.CurrentThread (). GetName () + "--dec--" + i);        }//Add thread class Inc implements Runnable {public void run () {INC ();        }}//Decrement thread class Dec implements runnable{public void run () {Dec ();                }} public static void Main (string[] args) {Test t = new Test ();        Instantiation of the Inner Class Inc inc. = T. New Inc ();               Dec Dec = t. New Dec ();            Create a 2*n thread here n=2 for (int i = 0; i < 2; i++) {New Thread (inc). Start (); New Thread (DEC). STart ();  }     } }




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.