Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any mistake in this Thread code.

 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You didn't read that part of Ulf's answer:
Ulf Dittmer wrote:You aren't starting NewThread
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must start() your anonymous NewThread object.
How are you learning about threads?

Jim ... ...
 
Christian Dillinger
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:You must start() your anonymous NewThread object.


NewThread is a Runnable so he cannot call start().
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is now executing, I missed this one

 
Christian Dillinger
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. Is it working now? If you wish, you
can post the entire program again for review.

Jim ... ...
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the correct code. Let em know it is efficient or not.
 
Paul Clapham
Sheriff
Posts: 21120
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic