> Java > java지도 시간 > 본문

Java에서 Yield() 메소드의 중요성은 무엇입니까?

WBOY
풀어 주다: 2023-09-07 21:05:02
앞으로
1386명이 탐색했습니다.

Java에서 Yield() 메소드의 중요성은 무엇입니까?

yield() 메서드는 Thread 클래스의 static 메서드로, 현재 실행 중인 스레드를 중지하고 동일한 우선순위의 다른 대기 스레드에 기회를 줄 수 있습니다. 대기 스레드가 없거나 모든 대기 스레드가 낮은 우선순위인 경우 동일한 스레드가 계속 실행됩니다. yield() 메서드의 장점은 대기 중인 다른 스레드를 실행할 기회가 있으므로 현재 스레드가 실행하고 프로세서를 다른 스레드에 할당하는 데 더 많은 시간이 필요한 경우입니다.

구문

public static void yield()
로그인 후 복사

class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; ++i) {
         Thread.yield(); // By calling this method, MyThread stop its execution and giving a chance to a main thread
         System.out.println("Thread started:" + Thread.currentThread().getName());
      }
      System.out.println("Thread ended:" + Thread.currentThread().getName());
   }
}
public class YieldMethodTest {
   public static void main(String[] args) {
      MyThread thread = new MyThread();
<strong>      </strong>thread.start();<strong>
</strong>      for (int i = 0; i < 5; ++i) {
         System.out.println("Thread started:" + Thread.currentThread().getName());
      }
      System.out.println("Thread ended:" + Thread.currentThread().getName());
   }
}
로그인 후 복사

출력

Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:Thread-0
Thread ended:main
Thread ended:Thread-0
로그인 후 복사

위 내용은 Java에서 Yield() 메소드의 중요성은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!