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

What does this thread do please?

 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

Ive never used thread before.

Ive found this thread that it looks like I need to use.

However I do not want to add something to my code if I do not understand it.

Could someone be kind enough to tell me line by line what it is doing?



I have looked over the web but cannot find anything that simple.

Thank you very much.

Darren
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'ed concentrate on posting something some code that compiles first, it looks like the code is tring to running myMethod in another thread but there is no definition of myMethod and I suspect its not a thread in its own right.

I suspect myMethhod isn't a thread and it should therefore be t.start () but again with out some code that will compile I can't explain what it 'll do when it runs. I'ed get the code to build and post that even if the code doesn't work as intended, its a lot easier to explain what it does as at the moment what its does line by line is fail to compile :-) . I suspect you want to define myMethod with in your new thread definition.

 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that should be t.start not myMethod.start.

The way I understand it is that the new thread is created, and then the run method tells the program what should be executed when that thread is run.

Then the t.start() actually starts the thread running, and will run the myMethod method.

Is this correct?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is correct.

Note that your t object is of an anonymous inner class, which is a subclass of Thread. As an instance of an anonymous inner class, it can execute the methods of the enclosing class. I am assuming myMethod() is a method of the enclosing class.

So, as you see, creating and starting a thread in Java is very easy. However, achieving successful concurrent operation, in any non-trivial application, is not at all easy.

Suddenly, things are quite different to how they were in your single-threaded program. In single-threaded code, if nothing in the currently-executing method says that object A should change, then object A won't change. In multi-threaded code, some other thread could change object A at any time. So you need to consider when this would cause problems, and use things like synchronized, wait() and notify() to avoid them.
 
Henry Wong
author
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I understand it is that the new thread is created, and then the run method tells the program what should be executed when that thread is run.

Then the t.start() actually starts the thread running, and will run the myMethod method.


You are creating an instance of an anonymous class, that inherits from the Thread class, and overrides the run() method. When the start() method of this Thread instance is called, a new thread will be created, which in turn, will call the run() method of the thread object.

However I do not want to add something to my code if I do not understand it.


It's great that you don't want to add something to code which you do not understand, but threading is a bit more complex than that. This new thread may be sharing variables with your main thread, may need to communicate with your main thread, etc.

Henry
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for the help guys.

Yes thread are pretty scary when you have not seen them before

Now I have a basic understanding of what they do I think I'll read up a little more on them.

Thanks

Darren
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic