Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread start() and run() help

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

I am currently working on a portion of a project using Threads and the Observer pattern, but I am running into some trouble with Threads. I am calling the start() method on my Thread but for some reason run() is never being called. According to the API for java.lang.Thread start() is supposed to make the JVM call the run() method. Any help would be great.

[EDIT] This is for a class so please just try to point me in the right direction, no freebies. Thanks




Hunter
 
Guido Granobles
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that when you initialize the thread printQueue is not empty. Beucase if so. The thread just finish the while loop and finish.
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:...I am calling the start() method on my Thread but for some reason run() is never being called...

Do you get any output in logs or anywhere related to this particular code?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The run() method is not being called for some reason. Even if printQueue were empty my debug statement "IN RUN" would print to the screen.
I placed a debug mark on the run() method and ran my debugger and that portion of the code is never entered.

Hunter
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Then what about the call to start the thread? Is that gets executed? Perhaps you can try some debugging code around that too...
 
Guido Granobles
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please, Can you post the code where you create the ClosingReportWriter object and start the thread.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my driver class:
I create the ClosingReportWriter here and start the new thread in the constructor for that class/



And this is the ClosingReportWriter class:

Where I called the start() method on the thread after it is created.




Thanks,
Hunter
 
Guido Granobles
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have screen.start() and file.start() in the main method. This

Does not make sense for me.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ClosingReportWriter class does not extend Thread. It implements runnable. So the writers are not threads and don't have a start() method.


Hunter
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:The ClosingReportWriter class does not extend Thread. It implements runnable...

So you could pass that runnable in to a Thread instance and start that thread. What you have done (as pointed out earlier) has no effect in the code.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I didn't even realize I wasn't doing that. That made the run() method execute. So I guess I was just restarting the main thread. Thanks Vijitha.

Hunter
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:...So I guess I was just restarting the main thread....

You were starting a different (new) thread which does nothing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic