This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Parking Lot problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Parking Lot problem" Watch "Parking Lot problem" New topic
Author

Parking Lot problem

Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Hey all

I have to build a project on a parking system. I have four lanes, with a maximum capacity of 10 vehicles each. I need to fill the four lanes concurrently. If the lanes are full then we need to notify other vehicles the same.

Can anyone help me with how to start with this problem?

thanks


OCPJP
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I feel you could have each of your vehicles run on a thread to the parking lot. The parking lot could be a simple Collection or a DB. This could be the initial draft. Could help you more if you find problem in implementation


Ashwin Sridhar
SCJP | SCWCD | OCA
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

Can anyone help me with how to start with this problem?


You know the requirement. You know what to do. Then, what do you want from us?

If you mean from where to start, I would suggest, based on your requirement choose a/some framework/s and make a blueprint (your software design).

Happy developing.

Regards,
Vishal.


Programming is about thinking, NOT coding
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Vishal,



I take one array as an input array which has all the vehicles in it.
Now, I take 4 arrays representing 4 different lanes.
Now i want to fill in those 4 lanes concurrently. This means i have to use 4 threads. Now tell me just one thing, How will a direct a thread to fill a particular array? like thread1 filling up array1. thread2 filling up array2 and so on. and what part of the code would i have to synchronize.
This is the point which is bugging me the most. Maybe its a very trivial thing, but i dont know why but i am not able to implement it..some help would be great!
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Ashwin Sridhar wrote:I feel you could have each of your vehicles run on a thread to the parking lot. The parking lot could be a simple Collection or a DB. This could be the initial draft. Could help you more if you find problem in implementation


refer to my previous reply and tell!
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Komal Arora wrote:Vishal,



I take one array as an input array which has all the vehicles in it.
Now, I take 4 arrays representing 4 different lanes.
Now i want to fill in those 4 lanes concurrently. This means i have to use 4 threads. Now tell me just one thing, How will a direct a thread to fill a particular array? like thread1 filling up array1. thread2 filling up array2 and so on. and what part of the code would i have to synchronize.
This is the point which is bugging me the most. Maybe its a very trivial thing, but i dont know why but i am not able to implement it..some help would be great!


Now that's better. Being direct to your point of problem

Coming to you problem
I take one array as an input array which has all the vehicles in it.

I would suggest use an array with 4 elements. Each element can be a Collection Object (List, Queue,Stack etc.) based on your requirement. Each of them representing a lane.

Now i want to fill in those 4 lanes concurrently. This means i have to use 4 threads

Why only 4 threads. It's not necessary to have 1 thread for each lane.

Identify the critical sections of your application like pushing car in a lane, popping car out of a lane etc. (forgive me for my wrong choice of word), and don't forget to synchronize them.
Then you can implement your logic , say if lane 1 is full goto lane2 etc.

I hope this helps you
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

This May help!

Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
sonu raj wrote:This May help!



Interesting, I could not find any new car entry module, or a car leaving module. In fact these things are totally based on requirement and there may be a lot more.
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

Vishal Shaw wrote:
Interesting, I could not find any new car entry module, or a car leaving module. In fact these things are totally based on requirement and there may be a lot more.


For that you should use collection or DB. I just gave a Demo as you asked where to synchronize.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

The way I would do this is use the producer-consumer pattern. You can have a a queue of cars. The main thread pushes the cars into the queue. Start 4 threads(a thread per lane) that looks at the queue, pops the first car out and puts in the car in it's own lane

This way all your doing is synchronizing on the queue when you pop a car out of the queue.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

@Sonu Raj,

Please, DontBeACodeMill. You can always suggest approaches at algorithmic level.

Also, I'm not sure why your code is having four methods which are almost same and four different classes for threads. How about different objects of same thread - with different input of course?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

Anayonkar Shivalkar wrote:@Sonu Raj,

Please, DontBeACodeMill. You can always suggest approaches at algorithmic level.



I am Sorry for that but I just gave a demo showing how can we use synchronized.

Next time I will keep in mind
and about the methods, a single method can also be used.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

I have implemented the approach suggested by one of the Ranchers. Could someone suggest if there is a better way to improve this code.

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

Ashwin, yeah that's the ticket!.

I would have just used the ThreadPoolExecutor. The code will be much smaller. However, the basic idea would be the same as you implemented.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Let me try to work on ThreadPoolExecutor. I am trying to convert this into more automatic system.

1. When parking lot is full, preventing further addition.

2. Allowing vehicles from VehicleQueue to park when some vehicle moves away from parking.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Parking Lot problem
 
Similar Threads
Designing traffic system
casting
Why don't we have public transport in smaller cities?
Fast and Furous, need turbo mode
Is this warning legally correct?