線程聯(lián)合
當前線程邀請調(diào)用方法的線程優(yōu)先執(zhí)行,在調(diào)用方法的線程執(zhí)行結(jié)束之前,當前線程不能再次執(zhí)行。線程A在運行期間,可以調(diào)用線程B的join()方法,讓線程B和線程A聯(lián)合。這樣,線程A就必須等待線程B執(zhí)行完畢后,才能繼續(xù)執(zhí)行。
join方法的使用
join()方法就是指調(diào)用該方法的線程在執(zhí)行完run()方法后,再執(zhí)行join方法后面的代碼,即將兩個線程合并,用于實現(xiàn)同步控制。
class A implements Runnable{
private Thread b;
public A(Thread b){
this.b = b;
}
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" A "+i);
if(i == 5){
try {
this.b.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class B implements Runnable{
@Override
public void run() {
for(int i=0;i<20;i++){
System.out.println(Thread.currentThread().getName()+" B "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestJoinThread {
public static void main(String[] args) {
Thread t1 = new Thread(new B());
Thread t = new Thread(new A(t1));
t.start();
t1.start();
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
if(i ==2){
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
線程聯(lián)合案例
需求:
實現(xiàn)爸爸讓兒子買煙。
/**
* 兒子買煙線程
*/
class SonThread implements Runnable{
@Override
public void run() {
System.out.println("兒子出門買煙");
System.out.println("兒子買煙需要10分鐘");
for(int i=0;i<10;i++){
System.out.println("第"+i+"分鐘");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("兒子買煙回來了");
}
}
/**
* 爸爸抽煙線程
*/
class FatherThread implements Runnable{
@Override
public void run() {
System.out.println("爸爸想抽煙,發(fā)現(xiàn)煙抽完了");
System.out.println("爸爸讓兒子去買一包紅塔山");
Thread t = new Thread(new SonThread());
t.start();
System.out.println("等待兒子買煙回來");
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("爸爸出門找兒子");
System.exit(1);
}
System.out.println("爸爸高興的接過煙,并把零錢給了兒子");
}
}
public class TestJoinDemo {
public static void main(String[] args) {
System.out.println("爸爸和兒子買煙的故事");
Thread t = new Thread(new FatherThread());
t.start();
}
}
Thread類中的其他常用方法
獲取線程名稱getName()
方式一
this.getName()獲取線程名稱,該方法適用于繼承Thread實現(xiàn)多線程方式。
class GetName1 extends Thread{
@Override
public void run() {
System.out.println(this.getName());
}
}
方式二
Thread.currentThread().getName()獲取線程名稱,該方法適用于實現(xiàn)Runnable接口實現(xiàn)多線程方式。
class GetName2 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
設(shè)置線程的名稱setName()
方式一
通過構(gòu)造方法設(shè)置線程名稱。
class SetName1 extends Thread{
public SetName1(String name){
super(name);
}
@Override
public void run() {
System.out.println(this.getName());
}
}
public class SetNameThread {
public static void main(String[] args) {
SetName1 setName1 = new SetName1("SetName1");
setName1.start();
}
}
方式二
通過setName()方法設(shè)置線程名稱。
class SetName2 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class SetNameThread {
public static void main(String[] args) {
Thread thread = new Thread(new SetName2());
thread.setName("SetName2");
thread.start();
}
}
判斷線程是否存活isAlive()
isAlive()方法: 判斷當前的線程是否處于活動狀態(tài)。文章來源:http://www.zghlxwxcb.cn/news/detail-745291.html
活動狀態(tài)是指線程已經(jīng)啟動且尚未終止,線程處于正在運行或準備開始運行的狀態(tài),就認為線程是存活的。文章來源地址http://www.zghlxwxcb.cn/news/detail-745291.html
class Alive implements Runnable{
@Override
public void run() {
for(int i=0;i<4;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestAliveThread {
public static void main(String[] args) {
Thread thread = new Thread(new Alive());
thread.setName("Alive");
thread.start();
System.out.println(thread.getName()+" "+thread.isAlive());
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread.getName()+" "+thread.isAlive());
}
}
到了這里,關(guān)于JAVA深化篇_29—— 線程使用之線程聯(lián)合以及Thread類中的其他常用方法【附有詳細說明及代碼案例】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!