0

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

Noel M
  • 15,812
  • 8
  • 39
  • 47
David Brown
  • 4,783
  • 17
  • 50
  • 75

2 Answers2

2

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
  • 5,535
  • 1
  • 24
  • 30
  • 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 Sep 07 '10 at 13:28
  • You are right, a member would be better if called multiple times: in this sample I went for the simplest way. – Guillaume Sep 17 '10 at 07:52
0

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.

Community
  • 1
  • 1
Noel M
  • 15,812
  • 8
  • 39
  • 47