Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent*;
- public class Pociąg implements Runnable{
- private String numer;
- public Pociąg{
- this.numer=numer;
- }
- public void operacja(String op, int czas){
- while(true){
- System.out.println(numer + " " + op);
- }try {
- Thread.sleep(czas);
- }catch(InterruptException e){}
- }
- public void run(){
- operacja("zbliża się do peronu",250 +(int)(Math.rand()*500);
- operacja("wjeżdża na tor",500 +(int)(Math.rand()*2500);
- operacja("odjeżdża",300 +(int)(Math.rand()*500);
- }
- }
- public class Peron
- public static void main(String args[]{
- int miejsca =2;
- int liczbaPociagow = 40;
- Pociąg[] pociag = new Pociąg[liczbaPociagow];
- ExecutorService kolejka = Executors.newFixedThreadPool(miejsca);
- for(int i=0;i=liczbaPociagow;i++){
- pociag[i]= new Pociag("Pociag" + (i+1));
- kolejka.execute(pociag[i]);
- }kolejka.shutdown();
- while(!kolejka.isTerminated()){
- SYstem.out.println("Dworzec zamknięty");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement