here is an example of what I mean:
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?
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.
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?
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..
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.
[ April 03, 2007: Message edited by: Nicole Lacoste ]