(6) How to share data among multiple threads (design 4 threads, where two threads add 1 to J at a time, and two threads reduce every 1)

Source: Internet
Author: User
Tags ticket

if each thread executes the same code, the same Runnable object can be used, and the Runnable object has that shared data, such as the ticket system.

Package com.itm.thread;

public class Multithreadsharedata {public

	
	static void Main (string[] args) {
		
		ShareData1 data1 = new ShareData1 (); c5/>//(1) method.
		new Thread (data1). Start ();
		
		New Thread (DATA1). Start ();
	}

}
	
	Class ShareData1 implements runnable{
		 	
		 	private int count =;
			@Override public
			Void Run () {while
				(true) {
				count--
				}
			}
		
		
		private int j = 0;
		public synchronized void increment () {
			j + +;
		}
		
		Public synchronized void Descrement () {
			j--;
		}
	}




if each thread executes different code how to solve it ...

Package com.itm.thread;
	
	public class MultiThreadShareData2 {private final static twosharedata data1 = new Twosharedata ();
		public static void Main (string[] args) {//This is shared data.
		
		Twosharedata data2 = new Twosharedata ();
		New Thread (New Onemyrunnable (DATA2)). Start ();
		
		New Thread (New Twomyrunnable (DATA2)). Start (); Local variables plus final.
		It can be done.
		Final Twosharedata data1 = new Twosharedata ();
		Runnable is the inner class.
			New Thread (New Runnable () {@Override public void run () {data1.descrement ();
		
		}). Start ();
			New Thread (New Runnable () {@Override public void run () {data1.increment ();
		
	}). Start ();
		} class Onemyrunnable implements runnable{private Twosharedata data1;
		Public onemyrunnable (Twosharedata data1) {this.data1 = data1;
		public void Run () {data1.descrement ();
		} class Twomyrunnable implements runnable{private Twosharedata data1; Public twomyrunnable (Twosharedata data1) {this.data1 = data1;
		public void Run () {data1.increment ();
		} class Twosharedata {private int j = 0;
		public synchronized void Increment () {j + +;
		Public synchronized void Descrement () {j--;
 }
	}

(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 each thread executes a different code, it requires a different runnable object,

There are two ways to implement data sharing between these runnable objects:
1, encapsulate the shared data in another object, and then pass the object to each Runnable object.

The way each thread operates on shared data is also assigned to that object, which makes it easy to implement mutexes and communications for each operation on that data.
2, these runnable objects as the internal classes in a class, the sharing of data as the member variables in this external class,

each thread's method of manipulating shared data is also assigned to an external class in order to achieve mutual exclusion and communication of each operation of the shared data .

These methods of calling external classes as individual runnable objects of the inner class.
3, the above two kinds of combination: the sharing of data encapsulated in another object,

The way each thread operates on the shared data is also assigned to that object to complete,

object as a local variable in the member variable or method in this outer class,

The Runnable object for each thread acts as the inner class or local inner class of the member inside the outer class.
4. In short, several pieces of code to synchronize mutual exclusion 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.



(3) An extreme and simple way of defining a static variable in any class that will be shared by all threads.

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.