3

I'm new to multithreading in android and java, I'm having difficult time implementing a simple model in which we can start worker thread and send some message or runnable to the worker thread from main thread where it does some operation and send result to main thread and updating ui thread

I tried this:

public class NewThreadUsingRunnable implements Runnable {

Handler handler;

@Override
public void run() {

    handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.i("Message Recieved", " " + msg);
            int result = 2+2
        }
    };
}

MainActivity:

new NewThreadUsingRunnable().run();
blackHawk
  • 6,047
  • 13
  • 57
  • 100
  • https://stackoverflow.com/questions/20054864/android-send-data-from-main-ui-thread-to-another-thread and in this blog explained, http://techtej.blogspot.in/2011/02/android-passing-data-between-main.html , i dont want to copy paste this. – Lingeshwaran May 29 '17 at 06:07
  • see https://stackoverflow.com/a/25096981/2252830 – pskink May 29 '17 at 06:22
  • you can use `HandlerThread` as pointed by @pskink – mallaudin May 29 '17 at 06:46
  • Why you deleted your answer that was helpful – blackHawk May 29 '17 at 07:33
  • its hard to understand in the post you referenced, because in the beginning i want to do communication between mainand worker threads using plain thread, handler and looper from scratch, I know i can use runonUi, async task, but i want to understand how these work collectively, instead handlerThread even it remove race condition issue – blackHawk May 29 '17 at 07:59
  • Not that much low level,I want to understanding by implementing how these things work and achieving particular task – blackHawk May 29 '17 at 08:05
  • I need a code example that uses vanila handler, looper to send message back and forth main thread – blackHawk May 29 '17 at 08:10
  • Your example just creates an object with a `run()`, method and then it calls the method. There's no threading there. Your `run()` method does even less: It creates an object and then it returns. The object that it creates is never used anywhere. If you're going to create a "Handler" with a "handleMessage()" method, then you probably will want to give the object to some other piece of code that will call `handleMessage(...)` when a message arrives. And, if you are going to create a `Runnable` object, then you probably will want to give the object to a `Thread` or to an `ExecutorService.` – Solomon Slow May 30 '17 at 17:19
  • Could you please answer – blackHawk May 30 '17 at 17:21
  • P.S.: A one way to pass objects between threads is to have the threads share a `Queue`. One thread puts objects into the queue, and the other takes them off. Use two queues if you need two-way communication. – Solomon Slow May 30 '17 at 17:21
  • Hard to answer. Your question is vague,... has something to do with threading, but there isn't even any attempt at threading in your example code. You probably should have a look at the [Java Concurrency Tutorial](https://docs.oracle.com/javase/tutorial/essential/concurrency/). – Solomon Slow May 30 '17 at 17:23
  • Its first time im trying threading, i just need to pass messages back and forth from main thread to worker thread, its like two way communication berween main and worker thread – blackHawk May 30 '17 at 17:26

1 Answers1

0

For those who wants Answer about this(because it has reached to popular question):

First of all you need to create Looper thread and have handler initialize along with Looper

Use Handler of worker/main thread and append message to it - In message object you can pass Constant, String and even bundle to and from worker thread

blackHawk
  • 6,047
  • 13
  • 57
  • 100