Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. package pomoc;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.concurrent.ThreadLocalRandom;
  5.  
  6. public class App extends Thread{
  7. public static ArrayList<String> lista;
  8. String name;
  9. int actiontype; //podajemy 1 jak watek ma dodawac, -1 jak usuwac
  10. int random;
  11. public App(String s, int type){this.name=s; this.actiontype=type; random = ThreadLocalRandom.current().nextInt(4000, 8000 + 1);}
  12.  
  13. public void run(){
  14.  
  15. while(true){
  16.  
  17. try {
  18. sleep(random);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. }
  22.  
  23. action();
  24. System.out.println(lista);
  25. }
  26. }
  27.  
  28. public synchronized void action(){
  29.  
  30. if(actiontype==1){lista.add(name);System.out.println("akcja add "+name); return;}
  31. if(actiontype==-1){
  32. if(!lista.isEmpty())
  33. lista.remove(0);
  34. System.out.println("akcja remove "+name);
  35. return;}
  36. else
  37. System.out.println("zly actiontype"+ name);
  38. }
  39.  
  40. public static void main(String[] args) {
  41. App.lista=new ArrayList<String>();
  42. App w1=new App("Thread1",1);
  43. App w2=new App("Thread2",1);
  44. App w3=new App("Thread3",-1);
  45.  
  46. App w4=new App("Thread4",1);
  47. App w5=new App("Thread5",-1);
  48. App w6=new App("Thread6",-1);
  49.  
  50. w1.start();
  51. w2.start();
  52. w3.start();
  53. w4.start();
  54. w5.start();
  55. w6.start();
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement