yield()让当前线程状态改为可运行状态,和其它线程一起竞争资源
代码示例如下:
public class Test68 extends Thread
{
@Override
public void run()
{
for (int i = 1; i <= 5; i++ )
{
try
{
Thread.sleep(500);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(i);
}
super.run();
}
public static void main(String[] args)
throws Exception
{
Test68 t1 = new Test68();
Test68 t2 = new Test68();
Test68 t3 = new Test68();
System.out.println("t1开始执行");