代码示例以下:编程
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开始执行");
t1.start();
// 当前线程状态改成可运行状态
t1.yield();
System.out.println("t2和t3开始执行");
t2.start();
t3.start();
}ide
}spa
运行结果:.net
t1开始执行
t2和t3开始执行
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5线程
代码示例以下:资源
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();
}get
public static void main(String[] args)
throws Exception
{
Test68 t1 = new Test68();
Test68 t2 = new Test68();
Test68 t3 = new Test68();
System.out.println("t1开始执行");
t1.start();
// 执行到1500ms后,再和其它线程一块儿竞争资源
t1.join(1500);
System.out.println("t2和t3开始执行");
t2.start();
t3.start();
}io
}class
运行结果:
t1开始执行
1
2
3
t2和t3开始执行
1
1
4
2
2
5
3
3
4
4
5
5
参照连接:https://www.jianshu.com/p/873f799153fb
参照来源:《Java多线程编程核心技术》