This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes What does this thread do please? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What does this thread do please?" Watch "What does this thread do please?" New topic
Author

What does this thread do please?

Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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

Joined: Oct 26, 2003
Posts: 413
    
    2

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.



"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does this thread do please?
 
Similar Threads
Reading from a socket
Calling Sleep with and without instance Query
JESS: running in a thread and more...
Question about the join() in thread
Unclear