File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parking Lot problem

 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashwin Sridhar
Ranch Hand
Posts: 272
Flex Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishal Shaw
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This May help!

 
Vishal Shaw
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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?
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
Flex Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
Flex Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic