Java-based multi-threading (3) Lockout

Source: Internet
Author: User

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

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.