---
title: "Thread.sleep() 和 Thread.yield() 区别"
author: "chenchendefeng"
date: "2009-08-25T11:02:25Z"
tags: []
slug: "thread-sleep-thread-yield"
draft: false
lastmod: "2009-08-25T11:02:25Z"
id: 457055
url: "https://chenchendefeng.iteye.com/blog/457055"
---

**1. Thread.yield():**

    api中解释： 暂停当前正在执行的线程对象，并执行**其他**线程。

    注意：这里的其他也包含当前线程，所以会出现以下结果。

 

01. **public** **class** Test **extends** Thread {
02. **public** **static** **void** main(String\[] args) {
03. **for** (**int** i = 1; i &lt;= 2; i++) {
04. **new** Test().start();
05. }
06. }
07. **public** **void** run() {
08. System.out.print("1");
09. yield();
10. System.out.print("2");
11. }
12. }

    输出结果： 1122 或者 1212

 

**2. Thread.sleep(long millis):**

 

     解释：使当前线程暂停millis所指定的毫秒，转到执行其它线程。