Archived

This topic is now archived and is closed to further replies.

[java] Thread Question

This topic is 5379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello; I''ll just get right to the point: If you pause a thread that is performing a long calculation inside of a for loop, which is looping over an array of objects (ie a Rectangle[]) and, if in another thread, the size and the contents of that array are changed and you notify the paused thread to continue, could there possibly be any runtime exceptions thrown? I know I could just do it and see if it happens, but that stage in the implementation phase is a ways off and I don''t really want any suprises, so I need to see if it''s not possible - so I can design a clean solution around it. Here are some things I do know about the problem: - the new array size will not be less than current_index+1 - The inner for loop''s (inside the for loop that loops over the array) bounds are dependent upon a member variable in the current object at current_index - I will be able to verify that the thread is effectively paused; so there isn''t a possibility that the thread could keep calculating when the other thread is resizing the array. Now, my initial guess at the answer to this problem would be "no" - there wouldn''t be a runtime exception since every object in java has an implicit pointer, and all I would be doing is making the array (which is an object) point to different location in memory. Since the new array size will not be smaller than current index (+1) that the for loop is at (same goes for the inner for loop), I will not get an ArrayIndexOutOfBoundsException. (remember this is a guess so i may be completely off ) Well I hope that made sense, and I gave enough information about my question. Thanks in advance for any insight you can offer. jp. ============================================== I feel like a kid in some kind of store... ============================================== www.thejpsystem.com

Share this post


Link to post
Share on other sites
My guess at the outcome is the same as yours - I don''t think your program will crash and burn. Even if it was going to throw an exception you could catch it if you are dead-set on this particular design.

However, I cannot possibly see why you would need to do this. It seems you don''t care about iterating through the entire array, so why are you doing that in the first place?

Share this post


Link to post
Share on other sites
Instead of using an array, why don''t you use a queue?

This way, when you are traversing the queue, you only check to see if there is another element, then process it. You don''t need to know how many there are at any given time. Since you said that it will only get bigger, a queue would model this exactly the way you want, without exceptions being an issue.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites
Hey guys;

Thanks for your responses. I actually didn''t really consider using a queue, it is a very good idea - which i''ll look into (however I may have to make a custom queue class).

btw,the reason why I need this functionality is because I am developing a distributed computing application in which there is a possibility where a computer finishes it''s task prematurely and I would like to give that computer some more data to process - which causes a shift in the other computers work loads (since that''s where the finished computer is taking its new data from).

Thanks again,
jp.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com

Share this post


Link to post
Share on other sites