代碼示例以下:編程
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多線程編程核心技術》