aspose file tools*
The moose likes Java in General and the fly likes for loop issue in thread application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "for loop issue in thread application" Watch "for loop issue in thread application" New topic
Author

for loop issue in thread application

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 126
Hi Folks,

I was coding and testing out a simple thread application when I realised that for some reason the program does not enter the for loop when it's supposed to. I am not sure why. I have placed print statements at selected points in both the java files to monitor the program flow, I can safely say the program execution goes all the way into the run method, but not into the for loop within the run method. I am not sure why, to me everything seems to be in order. Hope some can advise. Thanks.



NumberFinderThreadApp



Finder
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


Can you guess what this code is doing ?


[My Blog]
All roads lead to JavaRanch
Indrayani Godbole
Greenhorn

Joined: Oct 09, 2012
Posts: 26
Hi John,
I checked your code and found that Finder class constructor needs a change

your original constructor is :

Finder(int target, int begin, int end)
{
//Test
System.out.println("Finder constructor");
//end test

target = target;
begin = begin;
end = end;

}

here, you should initialize the field members begin and end as follows :


this.begin = begin;
this.end = end;

this will initialize your class field members begin and end which you are using in run method.

currently what is happening is, local variables begin and end of the constructor are getting initialized and so when run method is accessing the begin and end field members for the For loop, it is getting 0 values for both since they were not initialized through constructor.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Yes, you are actually working with variables local to your constructor, and class fields are left uninitialized.

@indrayani gd: Please UseCodeTags.


The quieter you are, the more you are able to hear.
John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 126
Hi Guys,

Thanks for the replies. Yes, 'this' does the trick. Thanks.

regards
John
 
 
subject: for loop issue in thread application