• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What does this thread do please?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic