raja ganeshan wrote:Create a class Fifo. It should have a Vector, and two methods addToVector() and removeFromVector(). In addToVector() keep a counter increment it every time and add the integer to the vector also print the number you are adding, then sleep for 100 millisec. In removeFromVector always remove the first element i.e. at 0 position and print the value removed, then sleep for 100 millisec. Create 2 threads implementing Runnable interface. two threads should keep calling addToVector. Another thread should keep calling removeFromVector. See what problems you face due to multi-threading.
Campbell Ritchie wrote:Why have you got an empty constructor? You ought to be initialising your Vector in the constructor.
You have been told to use very old-fashioned code. Vector and Enumeration have been considered legacy code for about 12 years.
Maybe the array out of bounds exception is one of the problems you are looking for. Or maybe not. You are creating two different Fifo objects, trying to fill one and empty the other
Dennis Deems wrote:One problem: Even though your Fifo class has a getInstance method, you are instantiating a new Fifo in both your threads. This is clearly not the intent of the exercise. Both threads are meant to use the same instance of Fifo. Another problem: the instructions could be written more clearly, but it seems to me that addToVector is meant to take an int parameter which is then added to the vector. A third problem: you are asked to increment the counter every time an int is added, yet you add 80 elements and increment the counter only once.
raja ganeshan wrote:wat i hav to change my code..
one confusion wher i initialize counter for increment..
Campbell Ritchie wrote:You have been told to use very old-fashioned code. Vector and Enumeration have been considered legacy code for about 12 years.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
raja ganeshan wrote:wat i hav to change my code..
That's the normal practise when it's wrong.
one confusion wher i initialize counter for increment..
Well, it depends what you're using it for. You do know that Vector has a size() method?
Campbell Ritchie wrote:You have been told to use very old-fashioned code. Vector and Enumeration have been considered legacy code for about 12 years.
True. However, this is an exercise to test concurrency, and Vector is already synchronized; so maybe that's why they chose it.
@Raja: Campbell is absolutely right about Enumeration though. Vector has been retrofitted to implement java.util.List, so there's really no need to use Enumerations. Use iterator() instead.
Winston
Campbell Ritchie wrote:We have been giving you ideas for days.
Agree, Winston, that might be why they chose Vector.
Campbell Ritchie wrote:But did they say why you were to use Vector? Was it because it is already synchronised, or are they simply very out of date?
raja ganeshan wrote:please give an idea for me...i changed but it wont work
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
raja ganeshan wrote:please give an idea for me...i changed but it wont work
So, what's the matter now?
This site is not in the business of handing out answers - especially for homework - and we would be doing you a disservice if we did.
I know it's frustrating, but you will learn a lot more if you try and fail; and the folks here are very patient if you're having problems.
So, once again...what problem are you having now? (and BTW, please show us your changed code.)
Winston