tags:

views:

99

answers:

2

Hi all!

I want to create one method and call the created Thread inside that method in my Android Application, I am new to Java as I had told before.

Can anybody giive me an example of how should I create a method and call a Thread inside that method!

Thanks, david

A: 

I'm not sure if it's a duplicate of this question, as I've not done any work on Android. But my answer on there will explain how to run a method inside a thread.

Noel M
+2  A: 

I'm not sure I understand your question. You want to start a thread inside a method?

The simplest way is:

public void myMethod() {
  new Thread().start();
}

How you might want to do something in this thread, which can be done this way:

public void myMethod() {
  new Thread(new Runnable(){
    public void run(){
      // do something here...
    }
  }).start();
}

Of course these anonymous objects can be expanded into full-fledged ones.

Guillaume Alvarez
This is the correct way for this to be done. For readability, I usually create the Runnable as a private member and then its reusable wherever it needs to be called in code by: new Thread(myRunnable).start;
Nick
You are right, a member would be better if called multiple times: in this sample I went for the simplest way.
Guillaume Alvarez