• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Protecting for loop values

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this for loop doesn't work how it should, but you can get the idea of what I want to do. What the loop is suppose to do is print the person's name in one System.out.print, and then the address in another. What my question is, let say I had a loop just like this. And I was running like 3 or 4 threads. How can I stop the treads from printing on the screen the same name/address that has already been written to the screen. Also how can I stop these 3 or 4 threads from skipping a name/address? Right now my array is small, but lets say it was larger, how can I stop these two things from happening? Thanks for any and all help!!

here is an example of what I mean:

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm not a guru regarding threads , but here is what i think. First of all if your code is executed by multiple threads you must synchronize your code, so that at any given time only one thread executes a piece of code (in your case the for loops in which you print the values). You want every thread to print an address?
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is almost certianly going to involve a share object with a synchronized get which dolls out the strings either by incrementing an internal index or owning the strings and returning them, also incrementing its internal counter.

The reason your not getting a lot of replies is your code is all over the place, trying running it, your throwing away your initialised array , try running it as is you'll get null null and I don't think thats the only issue. I sugggest you fix the bugs first so it works single threaded mode first and then repost.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I stop the treads from printing on the screen the same name/address that has already been written to the screen. Also how can I stop these 3 or 4 threads from skipping a name/address? Right now my array is small, but lets say it was larger, how can I stop these two things from happening? Thanks for any and all help!!



Is it really a thread issue? Could you dedup the array before starting the threads? Could it be done by a single thread?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a real simple solution to this.But I really wonder whether I have understood the question correctly.

My version goes like this :

1> Make a bean class which would have name and address as attributes.

2> Make another class (call it SharedData.java )which would have a syncronized method for getting a bean of type mentioned above.And after it gives out a bean it should point to the next bean.It is very simple to implement.You can have a arraylist as holder for the beans.

3> Make a thread that takes a object of above type (in constructor)and on run method you can get a object by quering the object reference that has been passed to this class in constructor and print name and address from that bean and ask for another bean and repeat the process.

4> Now write a class with main method which would first create an instance of SharedData Class and the create three threads with the same instance of the shared data in constructor and start all the threads.

That is all..
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Have a look at ConcurrentHashMap in java.util.concurrent. It is safe for multiple threads to add and remove elements. There is some very cool stuff in java.util.concurrent.



[URL=http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html]http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html


Niki
[ April 03, 2007: Message edited by: Nicole Lacoste ]
reply
    Bookmark Topic Watch Topic
  • New Topic