• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

for loop issue in thread application

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you guess what this code is doing ?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are actually working with variables local to your constructor, and class fields are left uninitialized.

@indrayani gd: Please UseCodeTags.
 
John Paterson
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

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

regards
John
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic