my dog learned polymorphism*
The moose likes Java in General and the fly likes please give me solution for below program 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 » Java in General
Bookmark "please give me solution for below program" Watch "please give me solution for below program" New topic
Author

please give me solution for below program

raja ganeshan
Greenhorn

Joined: Mar 23, 2012
Posts: 13
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

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.


Hi Raja. Welcome to The Ranch! But we don't just hand out answers like that round here. You have to do the work yourself, and we can help you with specific areas you have trouble understanding.

So. What have you done so far, and what problems are you having?
raja ganeshan
Greenhorn

Joined: Mar 23, 2012
Posts: 13


i wrote this program..but instance.removeFromVector(); here exception occur..(Array Index out of bound Exception)


[HENRY: Added Code Tags]
[BEAR: removed red from text]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
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
raja ganeshan
Greenhorn

Joined: Mar 23, 2012
Posts: 13
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



but my assignment was only for analysing synchronised block without this program...please give a good solution sir

[BEAR: removed red from text]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
There’s no need for red writing, thank you very much.

Stop trying to empty an empty object. Try to empty the full object. Then you can see which problems are related to concurrency.
You have already been told we don’t simply hand out answers.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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
Greenhorn

Joined: Mar 23, 2012
Posts: 13
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.


wat i hav to change my code..one confusion wher i initialize counter for increment..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
raja ganeshan
Greenhorn

Joined: Mar 23, 2012
Posts: 13
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


please give an idea for me...i changed but it wont work
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
We have been giving you ideas for days.

Agree, Winston, that might be why they chose Vector.
raja ganeshan
Greenhorn

Joined: Mar 23, 2012
Posts: 13
Campbell Ritchie wrote:We have been giving you ideas for days.

Agree, Winston, that might be why they chose Vector.


because my assignment was only on Vector...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
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
Greenhorn

Joined: Mar 23, 2012
Posts: 13
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?


its just an example...i only try by using Vector
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

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
raja ganeshan
Greenhorn

Joined: Mar 23, 2012
Posts: 13
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






thats my chaged code

[Jesper] (Please UseCodeTags)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
You have only answered half of what Winston asked. You didn’t say what the problem is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please give me solution for below program
 
Similar Threads
doubt in sleep() method
quick question
Begin threds through links
sleep??
Thread.sleep alters the output!