krtkush
krtkush

Reputation: 1528

Executing a runnable argument in a custom thread class

I want to define my runnable in my main thread and then pass it as an argument into another thread like below -

final Runnable runnable = new Runnable() {
            @Override
            public void run() {

                Log.i("Running runnable","");
            }
        };

Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        text.setText(msg.obj.toString());
    }
};

ThreadTest handlerThreadTest = new ThreadTest(runnable, handler);
                handlerThreadTest.start();

The ThreadTest class is defined as below -

public class ThreadTest extends Thread {

private Handler handler;
private Runnable runnable;

public ThreadTest(Runnable runnable, Handler handler) {

    this.handler = handler;
    this.runnable = runnable;
}

@Override
public void run() {
    super.run();

    runnable.run();

    Message msg = handler.obtainMessage();
    msg.obj = "YUSSSSSS!";
    handler.sendMessage(msg);
} }

I'm able to send the "YUSSSSSS" message to my handler but runnable.run() does not seem to be working because I see nothing in logcat.

Where am I getting this wrong?

Upvotes: 0

Views: 142

Answers (1)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Logcat tends to erase empty messages, add some characters to your log and you may see something under the TAG of "Running runnable"

Log.i("RunnableImpl", "Running runnable");

Upvotes: 1

Related Questions