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

📄 threadjoinandisalive.java

📁 《Java核心技术应用开发》电子工业出版社书籍源代码
💻 JAVA
字号:
package sample;
class Counter extends Thread {
      private int currentValue;

      public Counter(String threadName) {
	 super(threadName);
	 currentValue = 0;
	 System.out.println(this);
	 //System.out.println(Thread.currentThread());
	 setPriority(10);
	 start();
      }

      public void run() {
	 try {
	     while (currentValue < 5) {
		 System.out.println(getName() + ": " +
				    (currentValue++));
                 Thread.sleep(500);
             }
        } catch (InterruptedException e) {
	     System.out.println(getName() + " interrupted.");
        }
	System.out.println("Exit from " + getName() + ".");
     }
     public int getValue() { return currentValue; }
}


public class ThreadJoinAndIsAlive {
  public static void main(String args[]) {
      Counter cA = new Counter("Counter A");
      Counter cB = new Counter("Counter B");
      
      try {
	  System.out.println("Wait for the child threads to finish.");
	  cA.join();
	  cB.join();

	  if (!cA.isAlive())
	       System.out.println("Counter A not alive.");
          if (!cB.isAlive())
	       System.out.println("Counter B not alive.");
      } catch (InterruptedException e) {
	    System.out.println("Main Thread interrupted.");
      }
      System.out.println("Exit from Main Thread.");
  }
}

⌨️ 快捷键说明

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