简体   繁体   中英

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.

By this, you can have synchronization over the shared object to avoid classical reader writer problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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