Shared data between modules within a thread and independent data between threads

Source: Internet
Author: User

It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
How multiple threads access shared objects and data

1. If each thread executes the same code, the same Runnable object can be used, and the Runnable object has that shared data, for example, the ticket system can do so.
2. If the code executed by each thread is different, it is necessary to use different runnable objects in the following two ways to implement the data sharing between these runnable objects:
(1) Encapsulate the shared data in another object, and then pass the object one at a to each Runnable object. Each thread's method of manipulating the shared data is also assigned to that object to complete, which makes it easy to achieve mutual exclusion and communication for each operation that is performed on that data.
(2) Use these Runnable objects as internal classes in a class, share the data as member variables in the external class, and assign each thread to the external class how to manipulate the shared data to achieve mutual exclusion and communication for each operation on the shared data. These methods for external classes are called by each Runnable object that is an inner class.
(3) The combination of the above two ways: to encapsulate the shared data in another object, each thread to the operation method of the shared data is also assigned to the object to complete, the object as a member of the external class variables or local variables in the method, The Runnable object for each thread acts as a member inner class or local inner class in an external class.
(4) In short, it is best to synchronize mutually exclusive pieces of code in several separate methods, these methods are placed in the same class, it is easier to implement the synchronization of mutual exclusion and communication between them.
3. In an extreme and simple way, define a static variable in any class that will be shared by all threads.

Example Program:

Package Edu.review;
Import Java.util.HashMap;
Import Java.util.Map;

Import Java.util.Random; /** * Title: Constructs two threads, requires: * (1) Concurrent operation of two threads (this requires that the Syschronized keyword cannot be used) * (2) requires two threads to access their data separately, without interference (you can use the Map collection to Thread.curre Ntthread () as key, with data as value) * (3) The thread has a, b two modules, the module shared the information between **/public class Thread2threaddataindependent {STA
	Tic map<thread,integer> threadmap = new Hashmap<thread, integer> (); public static void Main (string[] args) {for (int i = 0; i < 2; i++) {New Thread (new Runnable () {@Overr IDE public void Run () {int data = new Random (). Nextint ();//The data here must be defined as a local variable, otherwise it will not be able to achieve the independence between the threads System.out.print
					ln (Thread.CurrentThread (). GetName () + "has put data:" +data);
					Threadmap.put (Thread.CurrentThread (), data);
					New A (). get ();
				New B (). get ();
		}}). Start ();
			}} static class a{public void Get () {int data = Threadmap.get (Thread.CurrentThread ()); System.out.println ("A from" +thread.currentthread (). GetName ()+ "Get Data:" +data);
			}} static class b{public void Get () {int data = Threadmap.get (Thread.CurrentThread ());
		System.out.println ("B from" +thread.currentthread (). GetName () + "Get Data:" +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.