Java multi-threaded print alternately switching waiting wake parity

introduction

     In their daily work life, it may be a few people or a lot of people doing the same thing in java programming, the same will occur when there will be a similar situation, a multiple threads do the same job, such as the train station can not buy a ticket system more than one person to buy a ticket is the same, when a window (thread) in selling a ticket when another window (thread) is not allowed to sell this ticket, and involves a lock in the process and the issue of resources waiting, how to correct thread to thread in the process of doing the same thing, do not grab a resource and has been waiting for a work situation, then we talk about multi-threaded wake-up wait and switch the process in this order on the two threads a and B alternately example of a printing parity for example, as follows:

package com.svse.thread;
import java.util.concurrent.atomic.AtomicInteger;

/ **
* Print alternating parity
* Function:
* @ author: ZSQ
* the Create DATE: 2019 Nian 5 afternoon of May 27 4:34:30
* Modifier modification times of description
*
* Copyright (c) 2019 Beijing Zhi Hua Tiancheng Technology Co., Ltd. - All Rights Reserved
* /
public class AlternatePrinting {

  // let two threads use the same lock. Alternately executed.
  // if the judgment is not odd odd odd to enter the print and add a thread of execution. Then thread releases the lock resources. Then let the thread waits
  // judge is not even, if an even number is even entering the print and add a thread of execution. Then thread releases the lock resources. Then let the thread waits
  public static of AtomicInteger AtomicInteger = new new of AtomicInteger ( . 1 );

  public static void main (String [] args) {
    the Thread A = new new the Thread ( new new AThread ());
    the Thread B = new new the Thread ( new new BThread ()) ;
    . A Start ();
    . B Start ();
  }

  //奇数线程
  public static class AThread implements Runnable{
    public void run() {
      while(true){
        synchronized (atomicInteger) {
        if(atomicInteger.intValue()%2 !=0){
          System.out.println("奇数线程:" + atomicInteger.intValue());
          try {
            Thread.sleep(500);
          } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
          atomicInteger.getAndIncrement(); // Atomically current value plus 1.
          // odd thread releases the lock resource
          . AtomicInteger Notify () ; // releasing the lock after performing the operation and waits
          the try {
            AtomicInteger. The wait ();
          } the catch (InterruptedException E) {
            e.printStackTrace ();
          }
        } the else {
          / / odd thread waits for
          the try {
            AtomicInteger. the wait ();
          } the catch (InterruptedException E) {
            // the TODO Auto-Generated Block the catch
            e.printStackTrace ();
          }
        }
      }
     }
    }
  }

 
   //偶数线程
  public static class BThread implements Runnable{
    public void run() {
      while(true){
       synchronized (atomicInteger) {
       if(atomicInteger.intValue()%2 ==0){
          System.out.println("偶数线程:"+ atomicInteger.intValue());
          try {
            Thread.sleep(500);
          } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
        atomicInteger.getAndIncrement(); // Atomically current value plus 1.
        // the even thread releases the lock resource
        . AtomicInteger Notify (); // After performing the operation to release the lock and enters a wait
        the try {
          . AtomicInteger the wait ();
        } the catch (InterruptedException E) {
          e.printStackTrace ();
        }
      } the else {
        the try {
          // wait for the even thread
          AtomicInteger. the wait ();
        } the catch (InterruptedException E) {
          // the TODO Auto-Generated Block the catch
          e.printStackTrace ();
        }
       }
      }
    }
    }
  }
}

 Results are as follows:

       

 

Guess you like

Origin www.cnblogs.com/zhaosq/p/10931071.html