⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 joindemo.java

📁 Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想
💻 JAVA
字号:
package chapter10;
public class JoinDemo
{
    public static void main(String args[])
    {
        NewThread nt1=new NewThread("First");
        NewThread nt2=new NewThread("Second");
        NewThread nt3=new NewThread("Third");
        //以下测试线程是否运行
        System.out.println("IsAlive(First):"+nt1.t.isAlive());
        System.out.println("IsAlive(Second):"+nt2.t.isAlive());
        System.out.println("IsAlive(Third):"+nt3.t.isAlive());
        try{
            System.out.println("Waiting for threads to finish.");
            //等待调用join()的线程直至结束
            nt1.t.join();
            nt2.t.join();
            nt3.t.join();
        }
        catch(InterruptedException e)
        {
           System.out.println("Main thread Interrupted");
        }
        //以下测试线程是否运行
        System.out.println("IsAlive(First):"+nt1.t.isAlive());
        System.out.println("IsAlive(Second):"+nt2.t.isAlive());
        System.out.println("IsAlive(Third):"+nt3.t.isAlive());
        System.out.println("Main thread exiting.");
    }
}
class NewThread implements Runnable
{
    String name;
    Thread t;
    NewThread(String th)
    {
        name=th;
        t=new Thread(this,name);
        System.out.println("New thread: "+t);
        t.start();
    }
    public void run()
    {
        try
        {
            for(int i=3;i>=0;i--)
                Thread.sleep(1000);
        }
         catch(InterruptedException e)
        {
            System.out.println(name+"Interrupted");
        }
        System.out.println(name+"  exiting.");
    }
}

⌨️ 快捷键说明

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