1. Latching Mode 1: Latching with Countdownlatch
Import Java.util.concurrent.countdownlatch;public class CloseLock3 {public static void main (string[] args) {Long start = S Ystem.currenttimemillis (); Countdownlatch latch = new Countdownlatch (5); Counteven even = new Counteven (latch), for (int i = 1; I <= 5; i++) {new Thread (even). Start ();} try {latch.await ();} catch (Interruptedexception e) {e.printstacktrace ();} Long end = System.currenttimemillis (); System.out.println ("An even number is:" + even.getnum ()); System.out.println ("Time-consuming:" + (End-start));}} Class Counteven implements Runnable {private int i = 100;private Boolean flag = true;private int num;private COUNTDOWNLATC H latch;public counteven (Countdownlatch latch) {super (); this.latch = latch;} @Overridepublic void Run () {try {while (flags) {synchronized (this) {if (i >= 0) {if (i% 2 = = 0) {System.out.println (Th Read.currentthread (). GetName () + ":" + i + "is an even number"); num++;} i--;} else {flag = false;}}}} finally {Latch.countdown ();}} public int Getnum () {return num;}}
2. Latching Mode 2: Latching using the return value of the callable
Import Java.util.concurrent.callable;import Java.util.concurrent.futuretask;public class Closelock {public static void Main (string[] args) {Long start = System.currenttimemillis (); Counteven even = new Counteven (); futuretask<integer> task = new futuretask<> (new callable<integer> () {@Overridepublic Integer call () Throws Exception {Even.even (); return null;}}); New Thread (Task, "Thread 1"). Start (); try {task.get ();//Blocking method} catch (Exception e) {e.printstacktrace ();} Long end = System.currenttimemillis (); System.out.println ("An even number is:" + even.getnum ()); System.out.println ("Time-consuming:" + (End-start));}} Class Counteven {private int i = 100;private Boolean flag = true;private int num;public void even () {while (flag) {Synchro Nized (This) {if (i >= 0) {if (i 2 = = 0) {System.out.println (Thread.CurrentThread (). GetName () + ":" + i + "is an even number"); nu m++;} i--;} else {flag = false;}}}} public int Getnum () {return num;}}
3. Latching using the IsAlive
public class Closelock {public static void main (string[] args) {Long start = System.currenttimemillis (); Counteven even = new Counteven (); Thread thread = new Thread (even), Thread.Start (), while (Thread.isalive ()) {//thread thread does not end up dead loop}long end = System. Currenttimemillis (); System.out.println ("An even number is:" + even.getnum ()); System.out.println ("Time-consuming:" + (End-start));}} Class Counteven implements Runnable {private int i = 100;private Boolean flag = true;private int num; @Overridepublic void Run () {while (flag) {synchronized (this) {if (i >= 0) {if (i 2 = = 0) {System.out.println (Thread.CurrentThread ()). Getna Me () + ":" + i + "is an even number"); num++;} i--;} else {flag = false;}}}} public int Getnum () {return num;}}
4. Using thread groups for latching
public class Closelock {public static void main (string[] args) {Long start = System.currenttimemillis (); Counteven even = new Counteven (); Threadgroup Group = new Threadgroup ("thread Group 1"); Thread thread = new Thread (Group,even); Thread thread1 = new Thread (group,even); Thread.Start (); Thread1.start (); while (Group.activecount ()!=0) {//activecount () method is mainly used to test}long end = System.currenttimemillis (); System.out.println ("An even number is:" + even.getnum ()); System.out.println ("Time-consuming:" + (End-start));}} Class Counteven implements Runnable {private int i = 100;private Boolean flag = true;private int num; @Overridepublic void Run () {while (flag) {synchronized (this) {if (i >= 0) {if (i 2 = = 0) {System.out.println (Thread.CurrentThread ()). Getna Me () + ":" + i + "is an even number"); num++;} i--;} else {flag = false;}}}} public int Getnum () {return num;}}
Java-based multi-threading (3) Lockout