- 1 sleep休眠的意思庶香,特點是在休眠的時候如果有鎖是不會釋放鎖的丑慎。
- 2 join例如t.join()意思是裹唆,阻塞當前調(diào)用線程号杏,直到線程t執(zhí)行完成婴氮。
- 3 yeild意思是讓出CPU讓其他線程執(zhí)行。
來分別看一下例子:
sleep:
package com.yuxi;
/**
* 線程休眠的例子
* Created by yuxi on 17/1/27.
*/
public class SleepDemo {
public static void main(String[] args) {
MyThread m = new MyThread();
//加鎖之后看如果一個線程進入休眠,是否其他的線程可以拿到這個鎖
new Thread(m).start();
new Thread(m).start();
new Thread(m).start();
}
static class MyThread implements Runnable {
public void run() {
synchronized (this) {
try {
System.out.println(Thread.currentThread().getName()+"當前線程休眠盾致。主经。。" + System.currentTimeMillis());
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+"當前線程休眠庭惜。罩驻。。" + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
結(jié)果是:
Thread-0當前線程休眠护赊。惠遏。。1485488800173
Thread-0當前線程休眠骏啰。节吮。。1485488805177
Thread-2當前線程休眠判耕。透绩。。1485488805177
Thread-2當前線程休眠壁熄。帚豪。。1485488810181
Thread-1當前線程休眠请毛。志鞍。。1485488810181
Thread-1當前線程休眠方仿。。仙蚜。1485488815185
yeild:
package com.yuxi;
/**
* yeild 高風亮節(jié)讓出cup
* Created by yuxi on 17/1/27.
*/
public class YeildDemo {
public static void main(String[] args) {
MyThread myThread = new MyThread();
new Thread(myThread).start();
new Thread(myThread).start();
}
static class MyThread implements Runnable {
public synchronized void run() {
for (int i = 0; i < 100; i++) {
if (i % 4 == 0) {
Thread.yield();
} else {
System.out.println("當前線程為此洲。。委粉。呜师。" + Thread.currentThread().getName());
}
}
}
}
}
結(jié)果是:
當前線程為。贾节。汁汗。衷畦。Thread-0
當前線程為。知牌。祈争。。Thread-0
當前線程為角寸。菩混。。扁藕。Thread-0
當前線程為沮峡。。亿柑。邢疙。Thread-0
當前線程為。橄杨。秘症。。Thread-0
當前線程為式矫。。役耕。采转。Thread-0
當前線程為。瞬痘。故慈。。Thread-0
當前線程為框全。察绷。。津辩。Thread-0
當前線程為拆撼。。喘沿。闸度。Thread-0
當前線程為。蚜印。莺禁。。Thread-0
當前線程為窄赋。哟冬。楼熄。。Thread-0
當前線程為浩峡。可岂。。红符。Thread-0
當前線程為青柄。。预侯。致开。Thread-0
當前線程為。萎馅。双戳。。Thread-0
當前線程為糜芳。飒货。。峭竣。Thread-0
當前線程為塘辅。。皆撩。扣墩。Thread-0
當前線程為。扛吞。呻惕。。Thread-0
當前線程為滥比。亚脆。。盲泛。Thread-0
當前線程為濒持。。查乒。弥喉。Thread-0
當前線程為。玛迄。由境。。Thread-0
當前線程為。虏杰。讥蟆。。Thread-0
當前線程為纺阔。瘸彤。。笛钝。Thread-0
當前線程為质况。。玻靡。结榄。Thread-0
當前線程為。囤捻。臼朗。。Thread-0
當前線程為蝎土。视哑。。誊涯。Thread-0
當前線程為挡毅。。暴构。慷嗜。Thread-0
當前線程為。丹壕。。薇溃。Thread-0
當前線程為菌赖。。沐序。琉用。Thread-0
當前線程為。策幼。邑时。。Thread-0
當前線程為特姐。晶丘。。。Thread-0
當前線程為浅浮。沫浆。。滚秩。Thread-0
當前線程為专执。。郁油。本股。Thread-0
當前線程為。桐腌。拄显。。Thread-0
當前線程為哩掺。凿叠。。嚼吞。Thread-0
當前線程為盒件。。舱禽。炒刁。Thread-0
當前線程為。誊稚。翔始。。Thread-0
當前線程為里伯。城瞎。。疾瓮。Thread-0
當前線程為脖镀。。狼电。蜒灰。Thread-0
當前線程為。肩碟。强窖。。Thread-0
當前線程為削祈。翅溺。。。Thread-0
當前線程為未巫。窿撬。。叙凡。Thread-0
當前線程為劈伴。。握爷。跛璧。Thread-0
當前線程為。新啼。追城。。Thread-0
當前線程為燥撞。座柱。。物舒。Thread-0
當前線程為色洞。。冠胯。火诸。Thread-0
當前線程為。荠察。置蜀。。Thread-0
當前線程為悉盆。盯荤。。焕盟。Thread-0
當前線程為廷雅。。京髓。。Thread-0
當前線程為商架。堰怨。。蛇摸。Thread-0
當前線程為备图。。。揽涮。Thread-0
當前線程為抠藕。。蒋困。盾似。Thread-0
當前線程為。雪标。零院。。Thread-0
當前線程為村刨。告抄。。嵌牺。Thread-0
當前線程為打洼。。逆粹。募疮。Thread-0
當前線程為。枯饿。酝锅。。Thread-0
當前線程為奢方。搔扁。。蟋字。Thread-0
當前線程為稿蹲。。鹊奖。苛聘。Thread-0
當前線程為。忠聚。设哗。。Thread-0
當前線程為两蟀。网梢。。赂毯。Thread-0
當前線程為战虏。拣宰。若未。冈爹。Thread-0
當前線程為。奋构。手趣。晌该。Thread-0
當前線程為。回懦。气笙。。Thread-0
當前線程為怯晕。潜圃。。舟茶。Thread-0
當前線程為谭期。。吧凉。隧出。Thread-0
當前線程為。阀捅。胀瞪。。Thread-0
當前線程為饲鄙。凄诞。。忍级。Thread-0
當前線程為帆谍。。轴咱。汛蝙。Thread-0
當前線程為。朴肺。窖剑。。Thread-0
當前線程為戈稿。苛吱。。器瘪。Thread-0
當前線程為翠储。。橡疼。援所。Thread-0
當前線程為。欣除。住拭。。Thread-0
當前線程為历帚。滔岳。。挽牢。Thread-0
當前線程為谱煤。。禽拔。刘离。Thread-0
當前線程為。睹栖。硫惕。。Thread-0
當前線程為野来。恼除。。曼氛。Thread-0
當前線程為豁辉。。搪锣。秋忙。Thread-1
當前線程為。构舟。灰追。。Thread-1
當前線程為狗超。弹澎。。努咐。Thread-1
當前線程為苦蒿。。渗稍。佩迟。Thread-1
當前線程為团滥。。报强。灸姊。Thread-1
當前線程為。秉溉。力惯。。Thread-1
當前線程為召嘶。父晶。。弄跌。Thread-1
當前線程為甲喝。。碟绑。俺猿。Thread-1
當前線程為。格仲。押袍。。Thread-1
當前線程為凯肋。谊惭。。侮东。Thread-1
當前線程為圈盔。。悄雅。驱敲。Thread-1
當前線程為。宽闲。众眨。。Thread-1
當前線程為容诬。娩梨。。览徒。Thread-1
當前線程為狈定。。习蓬。纽什。Thread-1
當前線程為措嵌。。芦缰。铅匹。Thread-1
當前線程為。饺藤。。流礁。Thread-1
當前線程為涕俗。。神帅。再姑。Thread-1
當前線程為。找御。元镀。。Thread-1
當前線程為霎桅。栖疑。。滔驶。Thread-1
當前線程為遇革。。揭糕。萝快。Thread-1
當前線程為。著角。揪漩。。Thread-1
當前線程為吏口。奄容。。锨侯。Thread-1
當前線程為嫩海。。囚痴。叁怪。Thread-1
當前線程為。深滚。奕谭。涣觉。Thread-1
當前線程為。血柳。官册。。Thread-1
當前線程為难捌。膝宁。。根吁。Thread-1
當前線程為员淫。。击敌。介返。Thread-1
當前線程為。沃斤。圣蝎。。Thread-1
當前線程為衡瓶。徘公。。鞍陨。Thread-1
當前線程為步淹。。诚撵。缭裆。Thread-1
當前線程為。寿烟。澈驼。。Thread-1
當前線程為筛武。缝其。。徘六。Thread-1
當前線程為内边。。待锈。漠其。Thread-1
當前線程為。。和屎。拴驮。Thread-1
當前線程為。柴信。套啤。。Thread-1
當前線程為随常。潜沦。。绪氛。Thread-1
當前線程為止潮。。钞楼。。Thread-1
當前線程為袄琳。询件。。唆樊。Thread-1
當前線程為宛琅。。逗旁。嘿辟。Thread-1
當前線程為。片效。红伦。。Thread-1
當前線程為淀衣。昙读。。膨桥。Thread-1
當前線程為蛮浑。。只嚣。沮稚。Thread-1
當前線程為。册舞。蕴掏。。Thread-1
當前線程為。囚似。剩拢。。Thread-1
當前線程為饶唤。徐伐。。募狂。Thread-1
當前線程為办素。。祸穷。性穿。Thread-1
當前線程為。雷滚。需曾。。Thread-1
當前線程為祈远。呆万。。车份。Thread-1
當前線程為谋减。。扫沼。出爹。Thread-1
當前線程為。缎除。严就。。Thread-1
當前線程為器罐。盈蛮。。技矮。Thread-1
當前線程為抖誉。。衰倦。袒炉。Thread-1
當前線程為。樊零。我磁。孽文。Thread-1
當前線程為。夺艰。芋哭。。Thread-1
當前線程為郁副。减牺。。存谎。Thread-1
當前線程為拔疚。。既荚。稚失。Thread-1
當前線程為。恰聘。句各。。Thread-1
當前線程為晴叨。诫钓。。篙螟。Thread-1
當前線程為。问拘。遍略。。Thread-1
當前線程為骤坐。绪杏。。纽绍。Thread-1
當前線程為蕾久。。拌夏。僧著。Thread-1
當前線程為。障簿。盹愚。。Thread-1
當前線程為站故。皆怕。毅舆。。Thread-1
當前線程為愈腾。憋活。。虱黄。Thread-1
當前線程為悦即。。礁鲁。盐欺。Thread-1
當前線程為。仅醇。冗美。。Thread-1
當前線程為析二。粉洼。。叶摄。Thread-1
當前線程為属韧。。蛤吓。宵喂。Thread-1
當前線程為。会傲。锅棕。。Thread-1
當前線程為淌山。裸燎。。泼疑。Thread-1
當前線程為德绿。。退渗。移稳。Thread-1
當前線程為。会油。秒裕。。Thread-1
當前線程為钞啸。几蜻。喇潘。。Thread-1
當前線程為梭稚。颖低。。弧烤。Thread-1
當前線程為忱屑。。暇昂。莺戒。Thread-1
其實執(zhí)行結(jié)果可以看出讓出CPU并不是說就一定會讓另外一個線程執(zhí)行,還是搶占式的急波。
join:
一道經(jīng)常出現(xiàn)的面試題,怎么保證T1,T2,T3三個同時啟動的線程按照順序執(zhí)行,例如順序T1,T2,T3
package com.yuxi;
/**
* 一道經(jīng)常出現(xiàn)的面試題,怎么保證T1,T2,T3三個同時啟動的線程按照順序執(zhí)行,例如順序T1,T2,T3
* Created by yuxi on 17/1/27.
*/
public class JoinDemo {
public static void main(String[] args) {
final Thread T1 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(10);
System.out.println("this is t1");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
final Thread T2 = new Thread(new Runnable() {
public void run() {
try {
T1.join();
Thread.sleep(10);
System.out.println("this is t2");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
final Thread T3 = new Thread(new Runnable() {
public void run() {
try {
T2.join();
Thread.sleep(10);
System.out.println("this is t3");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
T2.start();
T3.start();
T1.start();
}
}
結(jié)果是:
this is t1
this is t2
this is t3