Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1.  
  2. package watki1;
  3.  
  4. import java.util.ArrayList;
  5.  
  6. class Wspolny_zasob {
  7.  
  8.     ArrayList<String> komunikaty = new ArrayList<String>();
  9.  
  10.     synchronized void dodaj(String nowy) {
  11.         try {
  12.             if (komunikaty.size() > 0) {
  13.                 System.out.println("bwait1");
  14.                 wait();
  15.                 System.out.println("await1");
  16.             }
  17.             komunikaty.add(nowy);
  18.             System.out.println(komunikaty.toString());
  19.             Thread.sleep(3000);
  20.         } catch (Exception e) {
  21.         }
  22.     }
  23.  
  24.     synchronized void usun() {
  25.         try {
  26.             if (komunikaty.size() > 0) {
  27.                 komunikaty.clear();
  28.                 System.out.println("bnotify");
  29.                 notify();
  30.             }
  31.         } catch (Exception e) {
  32.         }
  33.     }
  34. }
  35.  
  36. class Watek1 implements Runnable {
  37.  
  38.     int i = 0;
  39.     Wspolny_zasob p1;
  40.  
  41.     public Watek1(Wspolny_zasob p) {
  42.         p1 = p;
  43.     }
  44.  
  45.     public void run() {
  46.         while (true) {
  47.             p1.dodaj("nowy" + (++i));
  48.         }
  49.  
  50.     }
  51. }
  52.  
  53. class Watek2 implements Runnable {
  54.  
  55.     Wspolny_zasob p1;
  56.  
  57.     public Watek2(Wspolny_zasob p) {
  58.         p1 = p;
  59.     }
  60.  
  61.     public void run() {
  62.         while (true) {
  63.             p1.usun();
  64.         }
  65.     }
  66. }
  67.  
  68. public class Watek_1 {
  69.  
  70.     /**
  71.      * @param args the command line arguments
  72.      */
  73.     public static void main(String[] args) {
  74.         // TODO code application logic here
  75.         Wspolny_zasob w1 = new Wspolny_zasob();
  76.         Thread wt1 = new Thread(new Watek1(w1));
  77.         wt1.start();
  78.         Thread wt2 = new Thread(new Watek2(w1));
  79.         wt2.start();
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement