aquaballoon

Java - Multi Thread

May 12th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. // Init.java
  2. package thread;
  3.  
  4. public class Init {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         new ThreadDemo("thread1").start();
  9.         new ThreadDemo("thread2").start();      
  10.     }
  11. }
  12.  
  13. // ThreadDemo.java
  14. package thread;
  15.  
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19. public class ThreadDemo extends Thread {
  20.  
  21.     String threadName;
  22.  
  23.     ThreadDemo(String threadName) {
  24.         this.threadName = threadName;
  25.     }
  26.  
  27.     public void run() {
  28.  
  29.         try {
  30.             for (int x = 0; x < 5; x++) {
  31.                 System.out.println(threadName + " " + x);
  32.                 Thread.sleep(5000);  // 5 sec                
  33.             }
  34.         } catch (Exception ex) {
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment