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"