java join yield_java中join和yield有什么区别?
sleep執(zhí)行后線程進(jìn)入阻塞狀態(tài)
yield執(zhí)行后線程進(jìn)入就緒狀態(tài)
join執(zhí)行后線程進(jìn)入阻塞狀態(tài)
main()
{
threadA.join(); //等線程A執(zhí)行完,再執(zhí)行主線程 .............
}
class BThread extends Thread
{
public BThread()
{
super("[BThread] Thread");
};
public void run()
{
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start.");
try
{
for (int i = 0; i < 5; i++)
{
System.out.println(threadName + " loop at " + i);
Thread.sleep(1000);
}
System.out.println(threadName + " end.");
} catch (Exception e)
{
System.out.println("Exception from " + threadName + ".run");
}
}
}
class AThread extends Thread
{
BThread bt;
public AThread(BThread bt)
{
super("[AThread] Thread");
this.bt = bt;
}
public void run()
{
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start.");
try
{
bt.join();
System.out.println(threadName + " end.");
} catch (Exception e)
{
System.out.println("Exception from " + threadName + ".run");
}
}
}
public class TestDemo
{
public static void main(String[] args)
{
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start.");
BThread bt = new BThread();
AThread at = new AThread(bt);
try
{
bt.start();
Thread.sleep(2000);
at.start();
at.join();
} catch (Exception e)
{
System.out.println("Exception from main");
}
System.out.println(threadName + " end!");
}
}
main start. //主線程起動(dòng),因?yàn)檎{(diào)用了at.join(),要等到at結(jié)束了,此線程才能向下執(zhí)行。[BThread] Thread start.
[BThread] Thread loop at 0
[BThread] Thread loop at 1
[AThread] Thread start. //線程at啟動(dòng),因?yàn)檎{(diào)用bt.join(),等到bt結(jié)束了才向下執(zhí)行。[BThread] Thread loop at 2
[BThread] Thread loop at 3
[BThread] Thread loop at 4
[BThread] Thread end.
[AThread] Thread end. // 線程AThread在bt.join();阻塞處起動(dòng),向下繼續(xù)執(zhí)行的結(jié)果main end! //線程AThread結(jié)束,此線程在at.join();阻塞處起動(dòng),向下繼續(xù)執(zhí)行的結(jié)果。
修改下代碼:
public class TestDemo
{
public static void main(String[] args)
{
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " start.");
BThread bt = new BThread();
AThread at = new AThread(bt);
try
{
bt.start();
Thread.sleep(2000);
at.start();
//at.join(); //在此處注釋掉對join()的調(diào)用 } catch (Exception e)
{
System.out.println("Exception from main");
}
System.out.println(threadName + " end!");
}
}
main start. // 主線程起動(dòng),因?yàn)門hread.sleep(2000),主線程沒有馬上結(jié)束;
[BThread] Thread start. //線程BThread起動(dòng)[BThread] Thread loop at 0
[BThread] Thread loop at 1
main end! // 在sleep兩秒后主線程結(jié)束,AThread執(zhí)行的bt.join();并不會影響到主線程。[AThread] Thread start. //線程at起動(dòng),因?yàn)檎{(diào)用了bt.join(),等到bt結(jié)束了,此線程才向下執(zhí)行。[BThread] Thread loop at 2
[BThread] Thread loop at 3
[BThread] Thread loop at 4
[BThread] Thread end. //線程BThread結(jié)束了[AThread] Thread end. // 線程AThread在bt.join();阻塞處起動(dòng),向下繼續(xù)執(zhí)行的結(jié)果
修改的例子也說明主線程的執(zhí)行結(jié)束,不影響子線程的繼續(xù)執(zhí)行。
-----------------------------------------------------------------------
Thread.yield()方法作用是:暫停當(dāng)前正在執(zhí)行的線程對象,并執(zhí)行其他線程。
yield()應(yīng)該做的是讓當(dāng)前運(yùn)行線程回到可運(yùn)行狀態(tài),以允許具有相同優(yōu)先級的其他線程獲得運(yùn)行機(jī)會。因此,使用yield()的目的是讓相同優(yōu)先級的線程之間能適當(dāng)?shù)妮嗈D(zhuǎn)執(zhí)行。但是,實(shí)際中無法保證yield()達(dá)到讓步目的,因?yàn)樽尣降木€程還有可能被線程調(diào)度程序再次選中。
結(jié)論:yield()從未導(dǎo)致線程轉(zhuǎn)到等待/睡眠/阻塞狀態(tài)。在大多數(shù)情況下,yield()將導(dǎo)致線程從運(yùn)行狀態(tài)轉(zhuǎn)到可運(yùn)行狀態(tài),但有可能沒有效果。
public class TestYield
{
public static void main(String[] args) throws InterruptedException
{
MyThread t1 = new MyThread("t1");
MyThread t2 = new MyThread("t2");
t1.start();
t2.start();
}
}
class MyThread extends Thread
{
MyThread(final String threadName)
{
super(threadName);
}
public void run()
{
for (int i = 1; i <= 100; i++)
{
if (i < 100 && getName().equals("t1"))
yield();
System.out.println(getName() + ":" + i);
}
}
}
并沒有保證2先于1執(zhí)行完畢
t1:1
t2:1
t1:2
t1:3
t2:2
t2:3
t2:4
t1:4
t1:5
t1:6
t2:5
t2:6
t2:7
t1:7
t1:8
t1:9
t2:8
t2:9
t2:10
t1:10
t1:11
t1:12
t1:13
t2:11
t2:12
t1:14
t1:15
t1:16
t2:13
t2:14
t1:17
t2:15
t2:16
t1:18
t1:19
t1:20
t1:21
t1:22
t1:23
t1:24
t2:17
t2:18
t2:19
t2:20
t2:21
t1:25
t1:26
t2:22
t1:27
t1:28
t2:23
t1:29
t2:24
t1:30
t1:31
t2:25
t1:32
t2:26
t1:33
t1:34
t2:27
t1:35
t2:28
t1:36
t1:37
t2:29
t1:38
t2:30
t1:39
t1:40
t2:31
t1:41
t2:32
t1:42
t1:43
t2:33
t1:44
t2:34
t1:45
t1:46
t2:35
t1:47
t2:36
t1:48
t1:49
t2:37
t1:50
t2:38
t1:51
t1:52
t2:39
t1:53
t2:40
t1:54
t1:55
t1:56
t1:57
t1:58
t1:59
t2:41
t1:60
t1:61
t2:42
t1:62
t2:43
t1:63
t2:44
t1:64
t2:45
t1:65
t2:46
t1:66
t2:47
t1:67
t2:48
t1:68
t2:49
t1:69
t2:50
t1:70
t2:51
t1:71
t2:52
t1:72
t2:53
t1:73
t2:54
t1:74
t2:55
t1:75
t2:56
t1:76
t2:57
t1:77
t2:58
t1:78
t2:59
t1:79
t2:60
t1:80
t2:61
t1:81
t2:62
t1:82
t2:63
t1:83
t2:64
t1:84
t2:65
t1:85
t2:66
t1:86
t2:67
t1:87
t2:68
t1:88
t2:69
t1:89
t2:70
t1:90
t2:71
t1:91
t2:72
t1:92
t2:73
t1:93
t2:74
t1:94
t2:75
t1:95
t2:76
t1:96
t2:77
t1:97
t2:78
t1:98
t2:79
t1:99
t2:80
t1:100
t2:81
t2:82
t2:83
t2:84
t2:85
t2:86
t2:87
t2:88
t2:89
t2:90
t2:91
t2:92
t2:93
t2:94
t2:95
t2:96
t2:97
t2:98
t2:99
t2:100
總結(jié)
以上是生活随笔為你收集整理的java join yield_java中join和yield有什么区别?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西门子逻辑运算指令_西门子S7-300P
- 下一篇: java的json解析工具_json文件