Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. package watki;
  2.  
  3.  
  4. class Thread03 implements Runnable  {
  5.      int liczba = 2;
  6.  
  7.      Object o = new Object();
  8.  
  9.      public void run( ) {
  10.        while (true) {
  11.            
  12.            synchronized (o) {
  13.                 liczba++;
  14.                 liczba--;
  15.            }
  16.            
  17.            
  18.         if (Thread.currentThread().isInterrupted())
  19.           return;
  20.         }
  21.      }
  22.  
  23.      public int getLiczba() {
  24.          
  25.          synchronized (o) {
  26.              return liczba;
  27.         }
  28.          
  29.     }
  30.  
  31. }
  32.  
  33. public class Main {
  34.  
  35.     public static void main(String[] args) throws InterruptedException {
  36.        
  37.         Thread03 r = new Thread03();
  38.         Thread t = new Thread(r);
  39.         t.start();
  40.         for (int i = 0; i < 1000000; i++) {
  41.             int l = r.getLiczba();
  42.             if (l % 2 != 0)
  43.                 System.out.println(l);
  44.         }
  45.         t.interrupt();
  46.          
  47.      }
  48.  
  49.  
  50.  
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement