• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threading assignment difficulty  RSS feed

 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings! This might be more appropriate for the threading forum, but I thought I'd try here first

I have an assignment on threads that's due in about 7 hours. My instructor knows I swear by JavaRanch, and I'm asking him the same question, but I'm hoping someone here might have an idea

The lab is supposed to be an application of Sun's Producer/Consumer Example, but there seems to be something I'm missing.

Here's the output, which hangs after the last line displayed here:

***Train number in the SET method is 1
Train 1 created

***Train number in the SET method is 2
Train 2 created

***Train number in the SET method is 3
Train 3 created

***Train number in the SET method is 4
Train 4 created
Train 1 waiting to enter the tunnel
Train 2 waiting to enter the tunnel
Train 3 waiting to enter the tunnel
Train 4 waiting to enter the tunnel

<snipped for lab integrity>

I notice, when looking at the Produce/Consumer portion of Sun's site, that
the get() returns a value after notifyAll().

When running in Debug through Netbeans, it looks like "available" never gets
touched, but I can't see why. Is it b/c I'm not doing a return? I can't figure out why it's just hanging.

Can anyone offer suggestions or advice?

Thanks!
[ July 09, 2004: Message edited by: dav mccartney ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about this: when the very first train gets to the tunnel, is the tunnel "available?"
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laugh... wow. Color me sleepdeprived.

Thanks Ernest. That was one of my "I thought I did that already" things.

Snipping the code out just for assignment integrity.
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears to work now... but is it really working? As I'm loading my trains to start them, I only create a new train object once, then use it over and over to start trains. Is this really working as it should? Is there a better way to do it?

Here's the code I'm referring to:



I guess, if it is working as it should... I don't quite understand why. My guess is that since I start the train thread before returning a new one, multiple instances are created? Just can't quite get my head around it.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like Train.readFromFile() is returning an instance of Train, most probably, a new instance each time it is invoked. If that's the case, the assignment of an instance of a Train to the reference t in the statement

Train t = new Train();

is pretty useless and might as well be simplified to just

Train t;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!