Advertisement
ibragimova_mariam

Untitled

Oct 23rd, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. private static class DoSmth{
  2.  
  3. private final MyClass a;
  4. private final MyClass b;
  5.  
  6. public DoSmth(){
  7. a = new MyClass("a");
  8. b = new MyClass("b");
  9. }
  10.  
  11. private void firstThread(){
  12. synchronized (a){
  13. b.print();
  14. }
  15. }
  16.  
  17. private void secondThread(){
  18. synchronized (b){
  19. a.print();
  20. }
  21. }
  22.  
  23. }
  24.  
  25. public static void main(String[] args) throws InterruptedException {
  26.  
  27. DoSmth ds = new DoSmth();
  28.  
  29. Thread thread1 = new Thread(new Runnable() {
  30. @Override
  31. public void run() {
  32. ds.firstThread();
  33. }
  34. });
  35.  
  36. Thread thread2 = new Thread(new Runnable() {
  37. @Override
  38. public void run() {
  39. ds.secondThread();
  40. }
  41. });
  42.  
  43. thread1.start();
  44. thread2.start();
  45. thread1.join();
  46. thread2.join();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement