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!!
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?
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.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
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?
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.