Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. package ss.week7.threads;
  2.  
  3. import java.util.concurrent.locks.*;
  4.  
  5. public class SynchronizedIntcell implements IntCell {
  6. private int value = 0;
  7. private boolean consumed;
  8.  
  9. public synchronized void setValue(int valueArg) {
  10. try{
  11. while(!consumed){
  12. wait();
  13. }
  14. } catch(Exception e){
  15. System.out.println(e.getMessage());
  16. }
  17. value= valueArg;
  18. consumed = false;
  19. notify();
  20. }
  21.  
  22. public synchronized int getValue() {
  23. try{
  24. while(consumed){
  25. wait();
  26. }
  27. } catch(Exception e){
  28. System.out.println(e.getMessage());
  29. }
  30. consumed = true;
  31. notify();
  32. return value;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement