Guest User

Untitled

a guest
Mar 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. public class Processor implements Runnable{
  2. private int id;
  3. public Processor(int id) {
  4. this.id = id;
  5. }
  6.  
  7. @Override
  8. public void run() {
  9. int count=0;
  10. System.out.println("Starting process id: " + id);
  11. while(count<100) {
  12. System.out.println("Pausing process id: "+id);
  13. try {
  14. wait();
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. notifyAll();
  19. System.out.println("Resuming process id: "+id);
  20. count++;
  21. }
  22. System.out.println("Completed process id: " + id);
  23. }
  24. }
  25.  
  26.  
  27.  
  28. public class Test {
  29. @SuppressWarnings("resource")
  30. public static void main(String[] args) {
  31. Scanner reader = new Scanner(System.in);
  32. System.out.print("Enter number of processes you want to create: ");
  33. int n = reader.nextInt();
  34. ExecutorService executor = Executors.newFixedThreadPool(n);
  35. for(int i=1;i<=n; i++) {
  36. executor.submit(new Processor(i));
  37. }
  38. executor.shutdown();
  39. try {
  40. executor.awaitTermination(10, TimeUnit.MINUTES);
  41. } catch (InterruptedException e1) {
  42. e1.printStackTrace();
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment