Advertisement
Utshaw

synchronizedBlockbyClassLevelLockWithTwoObjectTwoThreads

Oct 7th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. package intropackage;
  2.  
  3.  
  4. public class Driver {
  5. public static void main(String[] args) throws InterruptedException {
  6.  
  7. A a = new A();
  8. A a2 = new A();
  9. Display display = new Display(a,"FARHAN");
  10. Display display2 = new Display(a2 , "TANVIR");
  11. display.start();
  12. display2.start();
  13. }
  14. }
  15. public class A{
  16.  
  17. public void wish(String name)
  18. {
  19. for(int i=0;i<5;i++)
  20. {
  21. System.out.println("HABIZABI START BY "+Thread.currentThread());
  22. }
  23. synchronized (A.class) {
  24. for(int i=0;i<10;i++)
  25. {
  26. System.out.print("GOOD MORNING: ");
  27. try {
  28. Thread.sleep(1000);
  29. } catch (InterruptedException e) {
  30. System.out.println("INTERRUPTED");
  31. }
  32. System.out.println(name);
  33. }
  34. }
  35.  
  36. for(int i=0;i<5;i++)
  37. {
  38. System.out.println("HABIZABI END BY "+Thread.currentThread());
  39. }
  40. }
  41. public class Display extends Thread {
  42.  
  43. A object;
  44. String name;
  45. public Display(A object , String name) {
  46. this.name=name;
  47. this.object=object;
  48.  
  49.  
  50. }
  51. public void run()
  52. {
  53. object.wish(name);
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement