0

On Android I have a class MyThread that extends Thread. Inside MyThread I declare some Objects and initialize them inside the run() method. The Objects are instances of custom classes that have their own constructors. Some of them trigger another background Thread and accept listener Objects that are also initialized inside the run() method of MyThread. My problem is, that these listeners are executed on the main Thread instead of MyThread.

So my question is: on what Thread are the methods of custom Objects executed that are instantiated on a custom Thread?

1
  • probably the answer you seek is not found at the end of the question you have asked.
    – ataulm
    Jul 30, 2014 at 21:22

3 Answers 3

4

It doesn't matter on which threads objects are created, it matters from which thread a method upon the object is called.

For example:

class MyThread extends Thread {

    public MyObject myObject;

    @Override
    public void run() {
        myObject = new MyObject();

        /* Do stuff */
    }

}

Using this class:

MyThread myThread = new MyThread();
myThread.start();
myThread.myObject.someMethod();

Here, the someMethod call is done on the main thread.

2
  • 1
    I'd rephrase the "Using this thread" - it's ambiguous whether you mean that the three lines of code are executed on said thread, or whether you are describing the usage of said thread. -- +1 for edit
    – ataulm
    Jul 30, 2014 at 21:23
  • What I don't get is why e.g. the util.Log.d() of Android ist automatically executed on myThread while calling myObject.someMethod() (both called in run() ) is executed on main thread?
    – Juke
    Jul 30, 2014 at 21:36
1

It doesn't matter which thread they're instantiated on, objects (and thus your listeners) can be accessed from any of these threads that have a reference to them (whether it's safe to do so is another question).

The objects don't exist on any thread - when you call a method on an object from thread A, the code in that method will run on thread A, regardless of the thread in which the object was created.

0

When a program starts it's an empty room before a party. Then Alice comes in with her tote bag and starts cleaning things up. After some time, Alice rings Bob up and later Bob comes to help Alice in the kitchen. Alice and Bob prepare some sweets, and after a while Steve and Sarah knock at the door with some beer and chips.

Alice, Bob, Steve and Sarah are threads. Alice is the main thread because she organized the party. Sweets, beer and chips are the objects. Note that Steve brought the chips, but Alice can eat them, too! Sarah was responsible for the beers, but others can certainly drink. It doesn't matter who (which thread) brought what (created object) to the party: memory is shared among threds.

Now, let's examine how your Alice works. The main thread of an Android application is an event loop, that can be seen as:

while (running) {
  if (input != null) dispatch(input);
  sleepForSomeTime();
}

The call to dispatch() will finally reach your listeners code, so that it's executed on the same thread that handles the touch event. It doesn't matter who created the objects, who registered the listeners and so on.

I think it's not the case for your application, but one may actually need to process events in a pool of background threads. This is accomplished by giving your threads a queue of tasks to be done, and by having your event-driven code update that queue (or you can use the executors framework in java.util.concurrent)

As a note, you should not start threads inside your constructors because

  1. some objects may be seen before they are fully constructed (if you use an inner class or an anonymous one)
  2. the purpose of a constructor is to initialize fields. The creation of a background thread should be handled by the user of the newly constructed object
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.