Advertisement
Utshaw

multipleThreadsSameOnObject

Oct 7th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. package intropackage;
  2.  
  3.  
  4. public class Driver {
  5. public static void main(String[] args) throws InterruptedException {
  6.  
  7. Display display = new Display();
  8. myThread thread = new myThread(display);
  9. myThread2 thread2 = new myThread2(display);
  10. thread.start();
  11. thread2.start();
  12. }
  13. }
  14.  
  15.  
  16.  
  17. public class Display extends Thread {
  18.  
  19. public void displayN()
  20. {
  21. for(int i=0;i<10;i++)
  22. {
  23. System.out.println(i);
  24. try {
  25. Thread.sleep(1000);
  26. } catch (InterruptedException e) {
  27. System.out.println("INTERRUPTED");
  28. }
  29. }
  30.  
  31. }
  32. public void displayC()
  33. {
  34. for(int i=65;i<=75;i++)
  35. {
  36. System.out.println((char)i);
  37. try {
  38. Thread.sleep(1000);
  39. } catch (InterruptedException e) {
  40. System.out.println("INTERRUPTED");
  41. }
  42. }
  43.  
  44. }
  45.  
  46. }
  47.  
  48.  
  49. public class myThread extends Thread {
  50.  
  51. Display ob;
  52.  
  53. public myThread(Display ob) {
  54. this.ob=ob;
  55. }
  56. @Override
  57. public void run() {
  58. ob.displayN();
  59. }
  60.  
  61. }
  62.  
  63.  
  64. public class myThread2 extends Thread {
  65.  
  66. Display ob;
  67.  
  68. public myThread2(Display ob) {
  69. this.ob=ob;
  70. }
  71. @Override
  72. public void run() {
  73. ob.displayC();
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement