• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please give me solution for below program

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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]
 
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
raja ganeshan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have been giving you ideas for days.

Agree, Winston, that might be why they chose Vector.
 
raja ganeshan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have only answered half of what Winston asked. You didn’t say what the problem is.
 
reply
    Bookmark Topic Watch Topic
  • New Topic