Java-semaphore and Exchanger__java

Source: Internet
Author: User
SemaphoreControls the number of concurrent threads. Internal maintenance of a waiting queue, acquire the quota minus 1,release so that the quota plus 1.
Package Chapter01;

Import Java.util.concurrent.Semaphore; public class _01_run {/* * Multiple route-Multiple processing: * Waiting for threads in queue while processing Task/public static void main (string[] args) {Service s = n
		EW Service ();
		mythread []threads = new mythread[12];
			for (int i = 0; i < threads.length i++) {threads[i] = new Mythread (s);
		Threads[i].start ();
		} The static class Service {private semaphore s = new semaphore (3);
				void say () {try {s.acquire ();
				System.out.println ("ThreadName =" + Thread.CurrentThread (). GetName () + "in preparation");
				System.out.println ("Begin" + System.currenttimemillis ());
				for (int i = 0; i < 5 i++) {System.out.println (Thread.CurrentThread (). GetName () + print + (i+1));
				} System.out.println ("End" + system.currenttimemillis ());
				S.release ();
			System.out.println ("ThreadName =" + Thread.CurrentThread (). GetName () + "End"); The catch (Exception e) {//Todo:handle Exception}}} static class Mythread extends Thread {private Service s;
			Public Mythread (Service s) {super ();
		THIS.S = s;
		@Override public void Run () {S.say (); }
	}
}




Package Chapter01;
Import Java.util.concurrent.Semaphore;

Import Java.util.concurrent.locks.ReentrantLock;  public class _02_run {/* * Multiple route-Single processing: * Waiting for threads in queue, performing tasks in order (blocking) */public static void main (string[] args) {Service
		s = new Service ();
		mythread []threads = new mythread[12];
			for (int i = 0; i < threads.length i++) {threads[i] = new Mythread (s);
		Threads[i].start ();
		} The static class Service {private semaphore s = new semaphore (3);
		
		Private Reentrantlock lock = new Reentrantlock ();
				void say () {try {s.acquire ();
				System.out.println ("ThreadName =" + Thread.CurrentThread (). GetName () + "in preparation");
				Lock.lock ();
				System.out.println ("Begin" + System.currenttimemillis ());
				for (int i = 0; i < 5 i++) {System.out.println (Thread.CurrentThread (). GetName () + print + (i+1));
				} System.out.println ("End" + system.currenttimemillis ());
				Lock.unlock ();
				S.release (); System.out.println ("ThreadName =" + ThRead.currentthread (). GetName () + "End"); The catch (Exception e) {//Todo:handle Exception}}} static class Mythread extends Thread {private Serv
		
		Ice S;
			Public Mythread (Service s) {super ();
		THIS.S = s;
		@Override public void Run () {S.say (); }
	}
}




Exchange data between exchanger threads. The first line enters upgradeable exchanger, then waits for a thread to execute the exchanger, and then the data is exchanged.

Package Chapter01;

Import Java.util.concurrent.Exchanger; public class _03_run {* * * Exchange (): * 1. Thread A blocks * 2. Exchange data/public static void main (string[] args) {exc
		hanger<string> e = new exchanger<string> ();
		Threada a = new Threada (e);
		THREADB B = new threadb (e);
		A.start ();
	B.start ();
		
		Static Class Threada extends Thread {private exchanger<string> E;
			Public Threada (exchanger<string> e) {super ();
		THIS.E = e;
			@Override public void Run () {try {System.out.println ("a) gets the value of B:" + E.exchange ("a")); The catch (Exception e) {//Todo:handle Exception}}} static class Threadb extends Thread {private Excha
		
		Nger<string> e;
			Public threadb (exchanger<string> e) {super ();
		THIS.E = e;
			@Override public void Run () {try {System.out.println ("B") gets the value of a: "+ e.exchange (" B ")); catch (Exception e) {//Todo:handle Exception}}}




Recommended Bibliography:"Java programming Core method and framework"

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.