如何确保同步当一个线程不断将数据发送到另一个线程? [英] How to ensure synchronization when one thread keeps sending data to another thread?

查看:115
本文介绍了如何确保同步当一个线程不断将数据发送到另一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有2个线程:线程1和线程,除了主UI线程。

There are 2 threads: Thread1 and Thread2, in addition to the main UI thread.

UI线程产卵线程1。线程1 spwans线程2,并保持每隔一段时间发送阵列线程2。

The UI thread spawns Thread1. Thread1 spwans Thread2 and keeps sending an array to Thread2 at periodic intervals.

线程2应该继续听线程1只要收到数据,线程将处理它并将结果发送回UI线程。

Thread2 should keep listening to Thread1 and whenever it receives data, Thread2 will process it and send the result back to the UI thread.

如何才能实现这一目标?我开始用处理,但不知道如何线程2的处理程序发送到线程1,使线程1可以保持数据发布到线程2的消息队列。

How can this be achieved? I started off using handlers but did not know how to send Thread2's handler to Thread1, so that Thread1 can keep posting data to Thread2's message queue.

这是线程1:

public class Thread1 extends Thread {
Handler handler;
int a[] = {1,2,3,4,5,6,7,8,9,10};

@Override
public void run() {

    Thread2 thread2 = new Thread2();
    thread2.start();
    handler = thread2.getHandler();

    for(int i = 0; i < 5; i++){
        thread2.passData(a);
        handler.sendEmptyMessage(0);
        try{
            Thread.sleep(1000);
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

}

这是线程2:

public class Thread2 extends Thread {
private int a[];
Handler handler;

public void passData(int a[]) {
    this.a = new int[10];
    this.a = a.clone();
}

public Handler getHandler() {
    return handler;
}

@Override
public void run() {
    if(Looper.myLooper() == null) {
        Looper.prepare();
    }

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            for(int i = 0; i < a.length; i++) {
                //random work
            }
        }
    };
    Looper.loop();
}

}

推荐答案

假设线程1和线程是​​一个既 HandlerThread S,它们可以可以只用发送消息给对方对方的处理程序(S)。

Assuming Thread1 and Thread2 are a both HandlerThreads, they can can just send messages to each other using each other's Handler(s).

我开始用处理,但不知道如何线程2的处理程序发送到线程1,使线程1可以保持数据发布到线程2的消息队列。

I started off using handlers but did not know how to send Thread2's handler to Thread1, so that Thread1 can keep posting data to Thread2's message queue.

具体到这个问题,你可以创建一个包装handler.post()上HandlerThread2的方法,并呼吁从HandlerThread1的参考HandlerThread2。

Specific to this question, you can either create a method on HandlerThread2 that wraps the handler.post() and call that from HandlerThread1's reference to HandlerThread2.

//in HandlerThread2
public void send(SomeData data) {
    this.handler.post([runnable]);
}

或者你可以提供一个HandlerThread2访问从HandlerThread2得到实际的处理程序,并直接使用它。

Or you can provide an accessor in HandlerThread2 to get the actual handler from HandlerThread2 and use it directly.

//in HandlerThread2
public Handler getHandler() {
    return this.handler;
}

这篇关于如何确保同步当一个线程不断将数据发送到另一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆