Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package spo.labs.lab1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.Set;
  7.  
  8. public class ThreadMain {
  9.     private static int index = 0;
  10.  
  11.     public static void main(String[] args) throws InterruptedException {
  12.         Scanner reader = new Scanner(System.in);
  13.         char c = reader.next().trim().charAt(0);
  14.         List<Thread> threadList = new ArrayList<>();
  15.         CommonResource commonResource = new CommonResource();
  16.  
  17.         while (c != 'q') {
  18.             switch (c) {
  19.                 case '+':
  20.                     System.out.println("New Thread");
  21.  
  22. //                    thread.start();
  23.                     new Thread(new ChildThreads(commonResource)).setName("Thread " + ++index);
  24.                     Thread.currentThread().start();
  25.                     threadList.add(Thread.currentThread());
  26.                     break;
  27.  
  28.                 case '-':
  29.                     if (!threadList.isEmpty()){
  30.                         System.out.println("Kill Thread");
  31.                     }
  32.  
  33.                     break;
  34.  
  35.                 case 'a':
  36.                     System.out.println("All threads:\n");
  37.                     Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
  38.  
  39.                     for (Thread t : threadSet){
  40.                         System.out.println("Thread " + t + " state: " + t.getState());
  41.                     }
  42.  
  43.                     System.out.println("-----------------------------");
  44.                     for (Thread thread1 : threadList){
  45.                         System.out.println(thread1.getName() + " State:" + thread1.getState());
  46.                     }
  47.  
  48.             }
  49.             c = reader.next().trim().charAt(0);
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. package spo.labs.lab1;
  56.  
  57. public class CommonResource {
  58.     synchronized void showString() {
  59.         System.out.printf("%s \n", Thread.currentThread().getName());
  60.  
  61. //        try {
  62. //            Thread.sleep(100);
  63. //        } catch (InterruptedException e) {
  64. //            e.printStackTrace();
  65. //        }
  66.     }
  67. }
  68.  
  69.  
  70. package spo.labs.lab1;
  71.  
  72. public class ChildThreads implements Runnable {
  73.     CommonResource resource;
  74.  
  75.     public ChildThreads(CommonResource resource) {
  76.         this.resource = resource;
  77.     }
  78.  
  79.     @Override
  80.     public void run() {
  81.         resource.showString();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement