Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. package practice;
  2.  
  3. class NewThread implements Runnable {
  4. String name; // name of thread
  5. Thread t;
  6. boolean suspendFlag;
  7.  
  8. NewThread(String threadname) {
  9. name = threadname;
  10. t = new Thread(this, name);
  11. System.out.println("New thread: " + t);
  12. suspendFlag = false;
  13. t.start(); // Start the thread
  14. }
  15.  
  16. // This is the entry point for thread.
  17. public void run() {
  18. try {
  19. for(int i = 15; i > 0; i--) {
  20. System.out.println(name + ": " + i);
  21. Thread.sleep(200);
  22. Runtime r = Runtime.getRuntime();
  23. halt();
  24. }
  25. } catch (InterruptedException e) {
  26. System.out.println(name + " interrupted.");
  27. }
  28. System.out.println(name + " exiting.");
  29. }
  30.  
  31. synchronized void halt() throws InterruptedException
  32. {
  33. System.out.println(name + " entered synchronized halt");
  34. Runtime r = Runtime.getRuntime();
  35. Thread.sleep(1000);
  36. r.halt(9);
  37. System.out.println(name + " exiting synchronized halt"); // This should never execute
  38. }
  39. }
  40.  
  41. class Practice{
  42. public static void main(String args[]) {
  43. NewThread ob1 = new NewThread("One");
  44. NewThread ob2 = new NewThread("Two");
  45.  
  46. // wait for threads to finish
  47. try {
  48. System.out.println("Waiting for threads to finish.");
  49. ob1.t.join();
  50. ob2.t.join();
  51. } catch (InterruptedException e) {
  52. System.out.println("Main thread Interrupted");
  53. }
  54.  
  55. System.out.println("Main thread exiting."); // This should never execute
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement