Advertisement
ibragimova_mariam

Untitled

Oct 21st, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.concurrent.Executor;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.locks.ReentrantLock;
  8.  
  9. public class Synchronized {
  10.  
  11. static class Holder {
  12. private int value = 0;
  13. Object lockA = new Object();
  14. Object lockB = new Object();
  15.  
  16. ReentrantLock rLockA = new ReentrantLock();
  17. ReentrantLock rLockB = new ReentrantLock();
  18.  
  19.  
  20. synchronized void increment() {
  21. ++value;
  22. }
  23.  
  24. void doStuff() {
  25. rLockA.lock();
  26. // do thing A with mutual exlusion
  27. rLockA.unlock();
  28.  
  29. // do thing C
  30.  
  31. synchronized (lockB) {
  32. // do thing C with mutual exclusion <-- Thread A here
  33. }
  34. }
  35. }
  36.  
  37. static class Incrementor extends Thread {
  38. private final Holder h;
  39.  
  40. public Incrementor(Holder h) {
  41. this.h = h;
  42. }
  43.  
  44. @Override
  45. public void run() {
  46. for (int i = 0; i < 50000; i++) {
  47. h.increment();
  48. }
  49. }
  50. }
  51.  
  52. private static class Incrementor1{
  53.  
  54. private int counter;
  55.  
  56. private synchronized void increment(){
  57. counter++;
  58. }
  59.  
  60. public void showCounter(){
  61. System.out.println(counter);
  62. }
  63. }
  64.  
  65. public static void main(String[] args) throws InterruptedException {
  66.  
  67. // ProducerConsumer pd = new ProducerConsumer();
  68. Incrementor1 i1 = new Incrementor1();
  69.  
  70.  
  71. Thread thread1 = new Thread(new Runnable() {
  72. @Override
  73. public void run() {
  74. for (int i = 0; i < 1000; i++)
  75. i1.increment();
  76. }
  77. });
  78.  
  79. Thread thread2 = new Thread(new Runnable() {
  80. @Override
  81. public void run() {
  82. for(int i = 0; i < 1000; i++)
  83. i1.increment();
  84. }
  85. });
  86.  
  87. thread1.start();
  88. thread2.start();
  89. thread1.join();
  90. thread2.join();
  91.  
  92. i1.showCounter();
  93. }
  94.  
  95. private static class ProducerConsumer{
  96. private Queue<Integer> queue = new LinkedList<>();
  97. private final Object lock = new Object();
  98. int cnt = 0;
  99.  
  100. public void produce() throws InterruptedException {
  101. synchronized (queue) {
  102. while (true) {
  103. // synchronized (this) {
  104. if (queue.size() == 10) {
  105. queue.wait();
  106. }
  107. queue.offer(cnt++);
  108. queue.notify();
  109. // }
  110. }
  111. }
  112. }
  113.  
  114. public void consume() throws InterruptedException {
  115. synchronized (queue) {
  116. while (true) {
  117. //synchronized (this) {
  118. if (queue.size() == 0) {
  119. queue.wait();
  120. }
  121. System.out.println(queue.poll());
  122. System.out.println("size: " + queue.size());
  123. queue.notify();
  124. System.out.println("Hello!");
  125. // }
  126. Thread.sleep(1000);
  127. }
  128. }
  129. }
  130. }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement