multithreading - Multi-threading Java how to make thread to wait some time -


can me solve problem:

parking problem
"there's n parking lots. on 1 parking lot, there can 1 car @ time. if parking lots occupied car wait time , if there's still no free parking lot leaves."

it needs solved using threads (synchronized will).

here's code:
parking

class parking implements runnable { private thread thread; private string threadname; static int parkinglots;  static {     parkinglots = 5; }  parking(string threadname) {     this.threadname = threadname; }  public void run() {     if (parkinglots > 0) {         long resttime = (long) (math.random() * 2000);         try {             parkinglots--;             system.out.println("car " + threadname + " stands in parking lot");             thread.sleep(resttime);         } catch (interruptedexception e) {         }         parkinglots++;         system.out.println("car " + threadname + " has left parking, stood there" + ((double)resttime / (double)1000) + " s");     } else         system.out.println("car " + threadname + " has left parking"); }  public void start() {     if (thread == null) {         thread = new thread(this, threadname);         thread.start();     } } } 

main

public class main {     public static void main(string[] args) {         arraylist<parking> parking = new arraylist<parking>();          (int = 0; < 15; i++) {             parking.add(new parking(string.valueof(i + 1)));         }         (parking i: parking) {             i.start();         }     } } 

what want see (when there 2 parking lots , 4 cars):

car 1 stands in parking lot car 2 stands in parking lot car 3 waiting car 4 waiting car 3 has left parking car 2 has left parking, stood there 1.08 s car 4 stands in parking lot car 1 has left parking, stood there 1.71 s car 4 has left parking, stood there 0.83 s 

but (when there 2 parking lots , 4 cars): first cars (1 , 2) standing in parking , others (3 , 4) left it, because there no free parking lots. if there's 15 cars still can't in there.

so how can make cars wait time before leaving? if there's free parking lot go else gonna left parking.

any appreciated! thanks!

modified code, see if works.

public class parking implements runnable {     private thread thread;     private string threadname;     static int parkinglots;      static {         parkinglots = 5;     }      parking(string threadname) {         this.threadname = threadname;     }      public void run() {         long resttime = (long) (math.random() * 2000);         if (parkinglots > 0) {             checkparking();         } else {             try {                 system.out.println("car " + threadname + " waiting");                 thread.sleep(resttime);                 system.out.println("car " + threadname + " checking free parkinglot");                 checkparking();             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }      }      public void checkparking() {         if (parkinglots > 0) {         long resttime = (long) (math.random() * 2000);         try {             parkinglots--;             system.out.println("car " + threadname + " stands in parking lot");             thread.sleep(resttime);         } catch (interruptedexception e) {         }         parkinglots++;         system.out.println(                 "car " + threadname + " has left parking, stood there" + ((double) resttime / (double) 1000) + " s");      } else {         system.out.println(                 "car " + threadname + " has left since there no parking space");     }     }      public void start() {         if (thread == null) {             thread = new thread(this, threadname);             thread.start();         }     }  } 

public class main {

     public static void main(string[] args) {             arraylist<parking> parking = new arraylist<parking>();              (int = 0; < 15; i++) {                 parking.add(new parking(string.valueof(i + 1)));             }             (parking i: parking) {                 i.start();             }         } } 

output :

car 2 stands in parking lot car 1 stands in parking lot car 7 waiting car 5 stands in parking lot car 3 stands in parking lot car 6 waiting car 4 stands in parking lot car 9 waiting car 8 waiting car 10 waiting car 11 waiting car 12 waiting car 13 waiting car 14 waiting car 15 waiting car 4 has left parking, stood there0.049 s car 14 checking free parkinglot car 14 stands in parking lot car 5 has left parking, stood there0.366 s car 2 has left parking, stood there0.461 s car 12 checking free parkinglot car 12 stands in parking lot car 15 checking free parkinglot car 15 stands in parking lot car 1 has left parking, stood there0.882 s car 9 checking free parkinglot car 9 stands in parking lot car 10 checking free parkinglot car 10 has left since there no parking space car 3 has left parking, stood there1.014 s car 13 checking free parkinglot car 13 stands in parking lot car 15 has left parking, stood there0.937 s car 6 checking free parkinglot car 6 stands in parking lot car 11 checking free parkinglot car 11 has left since there no parking space car 13 has left parking, stood there0.344 s car 7 checking free parkinglot car 7 stands in parking lot car 8 checking free parkinglot car 8 has left since there no parking space car 7 has left parking, stood there0.054 s car 14 has left parking, stood there1.731 s car 9 has left parking, stood there1.359 s car 12 has left parking, stood there1.877 s car 6 has left parking, stood there1.787 s 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -