wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Is there any mistake in this Thread code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there any mistake in this Thread code." Watch "Is there any mistake in this Thread code." New topic
Author

Is there any mistake in this Thread code.

krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi everybody, I am Krishna, I have written this code on Threads, I don't know why this is not working, it is not giving a compile time error or run time error. The run() is not being executed.

The code is

Can anybody tell me what is wrong in this code. Thanks in advance. Have a nice day.


There is no rule that we all should know everything. Lets learn few things of everything here.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
You aren't starting NewThread, you're starting some other thread that does nothing. Also, remove lines 3 to 8 from the code; they're not needed.


Ping & DNS - my free Android networking tools app
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi Dittmer, I removed that lines and executed it again, but the run method is not being executed.
The output is 0123456789 with a delay of 500 milliseconds
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188


You didn't read that part of Ulf's answer:
Ulf Dittmer wrote:You aren't starting NewThread
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
You must start() your anonymous NewThread object.
How are you learning about threads?

Jim ... ...


BEE MBA PMP SCJP-6
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Jim Hoglund wrote:You must start() your anonymous NewThread object.


NewThread is a Runnable so he cannot call start().
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
The code is now executing, I missed this one

Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
That's a very uncommon use of threads. I'd recommend using the normal way... If you come to the chapter "executors" you'll understand why you should...
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Good point. Is it working now? If you wish, you
can post the entire program again for review.

Jim ... ...
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
This is the correct code. Let em know it is efficient or not.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

krishna anusha wrote:This is the correct code. Let em know it is efficient or not.


It would run much faster if you didn't have Thread.sleep all over the place. Or was that not what you meant by "efficient"?
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi Paul, thanks for the suggestion, I didn't get you, is the Thread.sleep() not efficient. Can you tell me how?

Thank you in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there any mistake in this Thread code.