File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "for loop issue in thread application" Watch "for loop issue in thread application" New topic

for loop issue in thread application

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 170
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.


Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Can you guess what this code is doing ?

[My Blog]
All roads lead to JavaRanch
Indrayani Godbole
Ranch Hand

Joined: Oct 09, 2012
Posts: 30
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)
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

Joined: Jun 19, 2010
Posts: 825

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: 170
Hi Guys,

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

I agree. Here's the link:
subject: for loop issue in thread application
It's not a secret anymore!