Advertisement
Josif_tepe

Untitled

Mar 30th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         MyThread t1 = new MyThread(1);
  4.         MyThread t2 = new MyThread(2);
  5.        t1.start();
  6.        t2.start();
  7.        try {
  8.            t1.join(500);
  9.            System.out.println(t1.isAlive());
  10.            t2.join(500);
  11.            System.out.println(t2.isAlive());
  12.        }
  13.        catch (InterruptedException ie) {
  14.            System.out.println(ie);
  15.        }
  16.     }
  17. }
  18. class MyThread extends Thread {
  19.     int id;
  20.     public MyThread(int _id) {
  21.         id = _id;
  22.     }
  23.     @Override
  24.     public void run() {
  25.  
  26.         for(int i = 0; i < 20; i++) {
  27.             System.out.println("Thread " + id + " " + i);
  28.             try {
  29.                 Thread.sleep(10);
  30.             }
  31.             catch (InterruptedException ie) {
  32.  
  33.             }
  34.         }
  35.  
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement