자바 다 중 스 레 드 전환 대기 깨 우기 교체 인쇄 짝수

머리말
     일상생활 에서 유용 할 때 몇 명 이나 많은 사람들 이 같은 일 을 할 수 있 습 니 다.자바 프로 그래 밍 에서 도 비슷 한 상황 이 발생 할 수 있 습 니 다.여러 스 레 드 가 같은 일 을 할 수 있 습 니 다.예 를 들 어 기차 역 에서 표를 사 는 시스템 은 여러 사람 이 같은 표를 살 수 없습니다.특정한 창(스 레 드)이 특정한 표를 팔 때 다른 창(스 레 드)은 이 표를 다시 팔 수 없습니다.이 과정 에서 하나의 자물쇠 와 자원 대기 문제 와 관련 되 었 습 니 다.스 레 드 와 스 레 드 가 같은 일 을 하 는 과정 에서 자원 을 빼 앗 지 않 고 계속 일 하 는 상황 을 기다 리 지 않 습 니 다.그 다음 에 다 중 스 레 드 의 대기 각성 과 전환 과정 에 대해 이야기 하 겠 습 니 다.여기 서 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 {
  //           。     。
   //                          。         。        
   //       ,                  。         。        
   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();
  }
  //    
   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();
        }
       }
     }
    }
  }
 }
}
 효 과 는 다음 과 같 습 니 다:

총결산
위 에서 말 한 것 은 소 편 이 여러분 에 게 소개 한 자바 가 다 중 스 레 드 전환 을 실현 하고 교체 인쇄 기 우 수 를 깨 우 는 것 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기