Java系列问题 —— join线程两个实例的两种写法

问题一:要求用到两个线程,一个线程想着白晶晶,另外一个线程想着紫霞仙子,要求至尊宝想着白晶晶的时候不能想着紫霞仙子。

//【第一种写法】
public class T1_One{
	public static void main(String[] args) throws Exception{
		new A().start();
	}
}
class A extends Thread{
	public void run(){
		String[] say = {"白晶晶","白晶晶你在哪?","白晶晶","月光宝盒"};
		for(int i=0;i<say.length;i++){
			if(!say[i].contains("晶晶") && !say[i].contains("白晶晶")){
				B x2 = new B();
				x2.start();
				try {
					x2.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else{
				System.out.println(say[i]);
			}
		}
	}
}
class B extends Thread{
	public void run(){
		System.out.println("紫霞仙子");
	}
}


//【第二种写法】
public class T3_Test extends Thread{
    public static void main(String[] args){
        System.out.println("至尊宝睡觉了");
        Thread thread1 = new Thread(){
            public void run() {
                System.out.println("白晶晶");
                System.out.println("白晶晶你在哪里?");
                System.out.println("白晶晶我要救你");
                System.out.println("月光宝盒");
            }
        };
        Thread thread2 = new Thread(){
            public void run(){
                System.out.println("紫霞仙子");
            }
        };
        try {
            thread1.start();
            thread1.join();
            thread2.start();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

问题二:要求用到两个线程,一个线程输出10次“紫霞仙子”,一个输出3次“白晶晶”。

//【第一种写法】
public class T2_Two {
	public static void main(String[] args) throws Exception{
		new B1().start();
	}
}
class B1 extends Thread{
	public void run(){
		for(int i=1;i<=10;i++){
			if(i == 4){
				B2 b2 = new B2();
				b2.start();
				try {
					b2.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(i+"紫霞仙子");
		}
	}
}
class B2 extends Thread{
	public void run(){
		for(int i=1;i<=3;i++){
			System.out.println(i+"白晶晶");
		}
	}
}


//【第二种写法】
public class T4_Test {
    public static void main(String[] args){
        Thread thread3 = new Thread(){
            public void run() {
                synchronized(this) {
                    for (int i = 0; i < 10; i++) {
                        System.out.println("紫霞仙子");
                        if (i == 3) {
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        };
        Thread thread4 = new Thread(){
            public void run() {
                synchronized (thread3) {
                    for (int i = 0; i < 3; i++) {

                        System.out.println("白晶晶");
                        try {
                            thread3.notify();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        thread3.start();
        thread4.start();
    }
}

猜你喜欢

转载自blog.csdn.net/Rao_Limon/article/details/88885981
今日推荐