Java 實現多線程切換等待喚醒交替打印奇偶數

引言java

     在平常工做生活中,可能會有用時幾我的或是不少人幹同一件事,在java編程中,一樣也會出現相似的狀況,多個線程幹一樣一個活兒,好比火車站買票系統不能多我的買一到的是同一張票,當某個窗口(線程)在賣某一張票的時候,別的窗口(線程)不容許再賣此張票了,在此過程當中涉及到一個鎖和資源等待的問題,如何合理正確的讓線程與線程在幹同一件事的過程當中,不會搶資源以及一個一直等待一個一直幹活的情況,接下來就聊一下多線程的等待喚醒以及切換的過程,在此就以A和B兩個線程交替打印奇偶數的例子爲例,代碼以下:編程

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

/**
* 交替打印奇偶數
*功能說明:
*@author:zsq
*create date:2019年5月27日 下午4:34:30
*修改人 修改時間 修改描述
*
*Copyright (c)2019北京智華天成科技有限公司-版權全部
*/
public class AlternatePrinting {atom

  //讓兩個線程使用同一把鎖。交替執行 。
  //判斷是否是奇數 若是是奇數進入奇數線程執行打印並加一。而後線程釋放鎖資源。而後讓該線程等待
  //判斷是否是偶數,若是是偶數進入偶數線程執行打印並加一。而後線程釋放鎖資源。而後讓該線程等待
  public static AtomicInteger atomicInteger =new AtomicInteger(1);

  public static void main(String[] args) {
    Thread a=new Thread(new AThread());
    Thread b=new Thread(new BThread());
    a.start();
    b.start();
  }

spa

  //奇數線程
  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(); // 以原子方式將當前值加 1。
          // 奇數線程釋放鎖資源
          atomicInteger.notify();//執行完操做後釋放鎖並進入等待
          try {
            atomicInteger.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }else{
          // 奇數線程等待
          try {
            atomicInteger.wait();
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            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(); // 以原子方式將當前值加 1。
        // 偶數線程釋放鎖資源
        atomicInteger.notify();//執行完操做後釋放鎖並進入等待
        try {
          atomicInteger.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }else{
        try {
          // 偶數線程等待
          atomicInteger.wait();
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
       }
      }
    }
    }
  }
}code

 效果以下:blog

       

相關文章
相關標籤/搜索