joindemo.java

来自「java 数据结构的一些简单实例和一些线程的应用主要是面向初学者」· Java 代码 · 共 60 行

JAVA
60
字号
class NewThread4 extends Thread
{
	String name;
	Thread t;
	
	NewThread4(String threadname)
	{
		name=threadname;
		t=new Thread(this,name);
		System.out.println("New thread:"+t);
		t.start();
	}
	
	public void run()
	{
		try
		{
			for(int i=5;i>0;i--)
			{
				System.out.println(name+":"+i);
				Thread.sleep(1000);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println(name+"exiting.");
		}
	}
}

public class JoinDemo 
{
     public static void main(String[] args)
     {
    	 NewThread4 ob1=new NewThread4("one");
    	 NewThread4 ob2=new NewThread4("two");
    	 NewThread4 ob3=new NewThread4("three");
    	 
    	 System.out.println("Thread one is alive:"+ob1.t.isAlive());
    	 System.out.println("Thread two is alive:"+ob2.t.isAlive());
    	 System.out.println("Thread three is alive:"+ob3.t.isAlive());
    	 
    	 try
    	 {
    		 ob1.t.join();
    		 ob2.t.join();
    		 ob3.t.join();
    	 }
    	 catch(InterruptedException e)
    	 {
    		 System.out.println("Main thread Interrupted");
    	 }
    	 
    	 System.out.println("Thread one is alive:"+ob1.t.isAlive());
    	 System.out.println("Thread two is alive:"+ob2.t.isAlive());
    	 System.out.println("Thread three is alive:"+ob3.t.isAlive());
    	 System.out.println("Main thread has finished");
     }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?