Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import java.util.concurrent*;
  2.  
  3. public class Pociąg implements Runnable{
  4. private String numer;
  5.  
  6.  
  7. public Pociąg{
  8. this.numer=numer;
  9.  
  10. }
  11.  
  12. public void operacja(String op, int czas){
  13. while(true){
  14. System.out.println(numer + " " + op);
  15.  
  16. }try {
  17. Thread.sleep(czas);
  18.  
  19. }catch(InterruptException e){}
  20.  
  21. }
  22.  
  23. public void run(){
  24.  
  25. operacja("zbliża się do peronu",250 +(int)(Math.rand()*500);
  26. operacja("wjeżdża na tor",500 +(int)(Math.rand()*2500);
  27. operacja("odjeżdża",300 +(int)(Math.rand()*500);
  28.  
  29. }
  30. }
  31.  
  32. public class Peron
  33.  
  34. public static void main(String args[]{
  35. int miejsca =2;
  36. int liczbaPociagow = 40;
  37.  
  38. Pociąg[] pociag = new Pociąg[liczbaPociagow];
  39. ExecutorService kolejka = Executors.newFixedThreadPool(miejsca);
  40. for(int i=0;i=liczbaPociagow;i++){
  41. pociag[i]= new Pociag("Pociag" + (i+1));
  42. kolejka.execute(pociag[i]);
  43.  
  44. }kolejka.shutdown();
  45. while(!kolejka.isTerminated()){
  46. SYstem.out.println("Dworzec zamknięty");
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement