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

📄 ex_18a.java

📁 JAVA分布式程序学习的课件(全英文)
💻 JAVA
字号:
// Based on Deitel text Fig. 13.3: PrintTest.java
// Show multiple threads printing at different intervals.

public class Ex_18a {
   public static void main( String args[] )
   {
      PrintThread thread1, thread2, thread3, thread4;

      thread1 = new PrintThread();
      thread2 = new PrintThread();
      thread3 = new PrintThread();
      thread4 = new PrintThread();

      thread1.start();
      thread2.start();
      thread3.start();
      thread4.start();
   }
}

class PrintThread extends Thread {
   int sleepTime;

   // PrintThread constructor assigns name to thread 
   // by calling Thread constructor
   public PrintThread()
   {
      // sleep between 0 and 5 seconds
      sleepTime = (int) ( Math.random() * 5000 );

      System.out.println( "Name: " + getName() +
                          ";  sleep: " + sleepTime );
   }

   // execute the thread
   public void run()
   {
      // put thread to sleep for a random interval
      try {
         Thread.sleep( sleepTime );
      }
      catch ( InterruptedException exception ) {
         System.err.println( exception.toString() );
      }

      // print thread name
      System.out.println( getName() );                             
   }
}

⌨️ 快捷键说明

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