aspose file tools*
The moose likes Java in General and the fly likes Thread start() and run() help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread start() and run() help" Watch "Thread start() and run() help" New topic
Author

Thread start() and run() help

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

"If the facts don't fit the theory, get new facts" --Albert Einstein
Guido Granobles
Greenhorn

Joined: Sep 11, 2010
Posts: 22
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.

http://guido-granobles.blogspot.com
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Mar 24, 2008
Posts: 3860

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

Joined: Sep 11, 2010
Posts: 22
please, Can you post the code where you create the ClosingReportWriter object and start the thread.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Sep 11, 2010
Posts: 22
You should have screen.start() and file.start() in the main method. This

Does not make sense for me.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Mar 24, 2008
Posts: 3860

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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Mar 24, 2008
Posts: 3860

Hunter McMillen wrote:...So I guess I was just restarting the main thread....

You were starting a different (new) thread which does nothing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread start() and run() help