• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multithreading  RSS feed

 
Shashankshanky Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create two threads. One to create an object and add it to list. Another one to modify the object in that list and add it to another list. Is it possible?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though I am a novice in threaded rpogramming, but I think what you want to achieve is not something unthinkable.
Synchronising the two methods would suffice I guess.
So you would write a class, have a list as its member, add one method which adds an object to this list and second method which
manipulates the object in the list....and having both methods synchronised so that at a time only one of these 2 methods is being
executed by any thread.
What I also see is that when you are adding in the list then add deep copy of the object so that object is now totally isolated from
outer world.

Manish
 
Shashankshanky Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done some research and found that it is producer consumer problem. But I am not able to understand the working.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashankshanky Gupta wrote:I have done some research and found that it is producer consumer problem. But I am not able to understand the working.


If the producer consumer pattern is what you need, then take a look at BlockingQueue based classes. There is even a very simple producer consumer example in the JavaDoc for it.,, The BlockingQueue takes care of all synchronization, and notifications for the full and empty conditions, so communications is simply put() and take().

Henry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!