Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I Messed Up With My Thread Code  RSS feed

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use Thread to generate some random numbers and I got some compilation errors:

.\CustomizedRandomNumGenerator.java:6: cannot resolve symbol
symbol : variable start
location: class CustomizedRandomNumGeneratorService
service = new CustomizedRandomNumGeneratorService().start;
^
.\CustomizedRandomNumGenerator.java:18: cannot resolve symbol
symbol : variable ranNum
location: class CustomizedRandomNumGenerator
return ranNum;

I have a main program that is supposed to display 5 random numbers:

The RandomNumberGenerator is an interface with a getRandom(); method to be implemented:

The CustomizedRandomNumGenerator implements the RandomNumberGenerator and starts a thread in the getRandom(); method. And it is the class where compilation errors occurred.

The code in the CustomizedRandomNumGeneratorService looks like:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goodness. You did say this had something to do with a job interview, didn't you? I suspect that you're going to have some real problems doing this job, if you get it. The answers to both of these are really very obvious.

Would you like to practice for your new job by thinking about these errors, and then posting your guesses in this thread -- or do you want us to just give you the answers? If you get a job as a programmer, you're going to have to get used to figuring this sort of thing out for yourself.

So what will it be?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint #1: What's the difference in syntax when referring to an object's variable and invoking an object's method?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm kinda with Ernest here. The code you have here shows many things, none of them good. My suggestion would be do delete the whole mess and spend a couple hours understanding how threads work.

P.S. the Thread.sleep(5) call in your code has no effect on your 'random number generater' thread.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come up with a clean skeleton (without compilation error). But, the code does not generate random numbers as it is coded:

and
 
Henry Wong
author
Sheriff
Posts: 22866
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: What is the variable that you are returning as a random number? And what is the variable you are using to generate the random number? ...

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!