简体   繁体   English

在另一个线程中使用一个线程中生成的数据

[英]Use data generated in one thread in another thread

Good morning, I'm having problems in my code I have a thread that generates a number according to the buttons are pressed and in another thread I would like to retrieve this value and send it by a method but I can't recover this value it is always reset to zero can I share this value between threads早上好,我的代码有问题 我有一个线程根据按下的按钮生成一个数字,在另一个线程中我想检索这个值并通过一个方法发送它,但我无法恢复这个值它总是重置为零我可以在线程之间共享这个值吗

First thread Generate data Values:第一个线程生成数据值:

Thread coletaDados = new Thread(new Runnable() {
    @Override
    public void run() {

                try {
                    while (true) {

                        dados = avancaEE + sobe + desce + avancaED + recuaED + recuaEE;

                     //   Log.i("Thread Dados", "Valor dos Dados" + dados);
                        Thread.sleep(100);
                    }
                }catch (InterruptedException e) {

                }
            }
        });

        coletaDados.start();


public class ThreadEnvio extends Thread {

byte[] EnvioManual = new byte[10];
ChecksumSend checksumSend = new ChecksumSend();
private  int Dados;
SerialPortManager spManager;
InseticidaManualFragment inseticidaManualFragment = new InseticidaManualFragment();

Second Thread use data value:第二个线程使用数据值:

public ThreadEnvio()
{
//    this.Dados = dados;
    spManager = SerialPortManager.getInstances();
}

@Override
public void run() {
    try {
        while (true) {

            Dados = inseticidaManualFragment.dados;
            //Dados = inseticidaManualFragment.dados;

            Log.d("Pressed", "dados" + Dados);

            EnvioManual[0] = (0x04);
            EnvioManual[1] = (0x10);
            EnvioManual[2] = (0x00);
            EnvioManual[3] = (0x00);
            EnvioManual[4] = (byte) 0xff;
            EnvioManual[5] = (0x00);
            EnvioManual[6] = (0x02);
            EnvioManual[7] = (byte) (Dados >> 8);
            EnvioManual[8] = (byte) (Dados & 0xff);
            EnvioManual[9] = checksumSend.Calculachecksum(EnvioManual);

            send(EnvioManual);

         //   Log.d("Saida", "Enviando" + toHexString(EnvioManual));
            Thread.sleep(100);
        }

    }
    catch (InterruptedException e) {
    }
}

public void send (byte[] x ){
    spManager.send(SerialPortManager.ttyS0,true,":" + toHexString(x) + "\r\n");
}
public static String toHexString(byte[] bytes){
    char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    char[] hexChars = new char[bytes.length*2];
    int v;
    for(int j=0;j<bytes.length;j++){
        v = bytes[j] & 0xFF;
        hexChars[j*2] = hexArray[v/16];
        hexChars[j*2 + 1] = hexArray[v%16];
    }
    return new String(hexChars);
}

} }

The best way which I think to share data across threads using a shared object where one thread will set the value and others will retrieve.我认为使用共享 object 跨线程共享数据的最佳方式,其中一个线程将设置值,其他线程将检索。

By this, you can have synchronization over the shared object to avoid classical reader writer problem.这样,您可以在共享的 object 上进行同步,以避免经典的读写器问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM