Advertisement
Josif_tepe

Untitled

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