Advertisement
Guest User

SEKSIK

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