Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I implemented runnable.... I think...

 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this project that I'm working with someone on. I'm worried that my code (which implements runnable) looks nothing like his. I'm thinking I did it wrong....

here it is:



[EDIT]And the following now works because of some other nice ranchers....


I think I didn't understand the interface well enough to do this..... I need help.
 
Paul Clapham
Sheriff
Pie
Posts: 20715
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah, you did implement Runnable. No doubt about that. That's a perfectly good implementation of Runnable. But, this code here:

It's a lie. That's what you're missing, I think, you don't actually create any new threads. So somewhere you'll need to create new Threads which use those Runnable objects, and start those threads.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't mean to lie.....

I just guess I dunno how to use the interface correctly. It says in the API that:
void run()When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
The general contract of the method run is that it may take any action whatsoever.


I guess I just don't know what I'm doing.

how does JVM know I want a thread? I mean, without extending Thread if that's possible, which was not a part of the spec of the project.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright... I thought I haf it figured out, but it's not working the way I'd like....



When I type 0 to exit the app, it gives an array index out of bounds runtime error....

I'm still not sure I've done this right. I would have liked to loop through, get all the inputs and output each to a new thread and start them all at once. Maybe name them on the fly too.... can I do that?

 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try moving the prompt to enter numbers to the end of the loop.

And have a look at this thread from last year. Note the strange-looking

while (!(name = input.nextLine()).equalsIgnoreCase("stop")) . . .

syntax. Count the number of round brackets/parentheses and see whether I have balanced them at all. Go through that complicated bit of code, work out what it does, work out why you need an extra pair of () round the = sign. It might look strange, and as I told RL your teacher will never believe you if you tell him you worked it out for yourself.

But it does work. It may allow you to use a while loop instead of a do-while loop, in many cases.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:I didn't mean to lie.....
He never said you were lying. He said the program was lying.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try moving the prompt to enter numbers to the end of the loop.

And have a look at this thread from last year. Note the strange-looking

while (!(name = input.nextLine()).equalsIgnoreCase("stop")) . . .

syntax. Count the number of round brackets/parentheses and see whether I have balanced them at all. Go through that complicated bit of code, work out what it does, work out why you need an extra pair of () round the = sign. It might look strange, and as I told RL your teacher will never believe you if you tell him you worked it out for yourself.

But it does work. It may allow you to use a while loop instead of a do-while loop, in many cases.


Alright, I think you're confusing me with RL if you think this will be a "complicated bit of code"

Ahem.... the extra set of parenthesis is for order of operation. The order of that line is....

Dear JVM,
1. Find the nextLine from the input scanner
2. When you get that, set 'name' equal to that.
3. Figure out if it's the same as "stop"..... go ahead and ignore the case.
4. Reverse the boolean value of #3
5. Continue on while true.
Thanks

As for my application, if you're gonna dredge up the thread , I may as well post what I handed in.




I think I did a good job of making sure the program was not lying anymore. I washed its mouth out with soap.

Thanks, Campbell, for the pointer on avoiding the do/while.... and bringing my dead and unanswered thread back to life.
There are 2 do/while loops in my code. I understand why it's important to know how they differ from a plain old while loop. I don't understand why people shy away from using them. I would like to wager most things SHOULD run at least once before terminating the loop.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
Yes, most things ought to run at least once, but you can usually fiddle it so the continuation condition comes to true at the beginning of a while loop. So do loops are used much less than while and for loops.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic