开发者

Threads from AsyncTask block UI

I have MainActivity which does some Work before it Executes an AsyncTask called "Datensammlung". This task starts some other Threads via different classes. All of Them implement Runnable and work correct. Two are for communication with a Server(TCP Connections) and some are listening for Events/ generating random numbers(Intervall 10 seconds).

Now i want to display some Values every thread works on(i always use synchronized). When i only start the Listener-Threads, "onProgressUpdate" is called maybe 5 times until it ends updating the UI. When i start the two other threads for Communication nothing is displayed ever.

Why is my UI still blocked although i used asynctasks?

Anyone got an idea? Thank you!

Fabian

AsyncTask:Datensammlung

protected Void doInBackground(String[]... params) {
    // TODO Auto-generated method stub
    while (true) {
        int counter = 0;
        ArrayList<String> texte = new ArrayList<String>();
        String test = "";
        for (Input i : this.Dataliste) {

            String text = " "+i.variablenName + ": "+String.valueOf(i.getAbstrakterWert())+"\n";
            texte.add(text);
            test += text;
        //  Log.e("TEXT ", text);
        //  counter ++;
        }

        publishProgress(test);
        Log.e("TEXT", test);
        test = "";
        counter ++;

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO: handle exception
        }
    }

}
@Override
protected void onProgressUpdate(String... listen) {
    TextView t = this.viewList.get(0);
    Log.e("hier isser", "1");
    for (String r : listen) {
        t.setText(r);
        Log.e("hier isser", r);
    }

}

One of my Communication Class:

package kommunikation;


public class SensorAdapter implements Runnable{
    String iP;
    int port;
    Socket socket;
    ObjectOutputStream out;
    ObjectInputStream in;
    ArrayList<Nachricht> nachrichtenliste = new ArrayList<Nachricht>();

    Handler handler = new Handler();
    // Konstruktor
    public SensorAdapter(String iP, int port) {
        super();

        this.iP = iP;
        this.port = port;
    }

    public boolean initialisiere_sensor(ArrayList<Textobjekt> pObjekte){
        try {
            socket = new java.net.Socket(iP,port);
            // serialisiere alle Inputs und sende die Daten an das FW
            out = new ObjectOutputStream(new ObjectOutputStream(socket.getOutputStream()));
            out.writeObject(pObjekte);
            out.flush();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        return true; 
    }

    public void run() {

        try {
            while (true) {
                if (!nachrichtenliste.isEmpty()) {
                    PrintWriter printWriter =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                    Nachricht speicher = nachrichtenliste.get(0);
                    String senden = schreibe_nachricht(speicher);


                        printWriter.print(senden);
                        printWriter.flush();
                        synchronized (nachrichtenliste) {
                            nachrichtenliste.remove(speicher);
                        }

                }
                try {
                    Thread.sleep(500);
                    handler.post(this);
              开发者_运维技巧  } catch (InterruptedException e) {
                    // TODO: handle exception
                }

            }


        } catch (Exception e) {
            // TODO: handle exception
        }       
    }

The Place where the Communication-Thread gets started:

public class Kommunikator implements Callback{

    ArrayList<Input> objektliste;
    ArrayList<Textobjekt> textliste;
    boolean update_erforderlich = false;
    public boolean bereit = false;
    private Verbindungsdaten verbindungsdaten;
    private SensorAdapter sadapter;
    private ClientAdapter cadapter;
    Thread sensorfred;
    Thread clientfred;



    // Konstruktor
    public  Kommunikator(ArrayList<Input> plist, ArrayList<Textobjekt> ptextliste){

        boolean check;
        boolean cCheck;
        this.objektliste = plist;
        this.textliste = ptextliste;
        // startet die kommunikation
        this.sadapter = new SensorAdapter("192.168.2.106", 1111);
        this.cadapter = new ClientAdapter("192.168.2.106", 2222,this);
    check = sadapter.initialisiere_sensor(ptextliste);


    if (check ) {
        sensorfred = new Thread(sadapter);
        sensorfred.start();

    }
    // client darf wirklcih erst nach dem sensorlayer starten
    cCheck = cadapter.initialisiere_client(ptextliste);
    if (cCheck) {
        clientfred = new Thread(cadapter);
        clientfred.start();
    }
    this.bereit = true;
    }
    // kann vom Sensor aufgerufen werden um die updates an das framework zu senden
    public void melde(Nachricht na){
            Nachricht speicher =null;
            for (Nachricht n : this.sadapter.nachrichtenliste) {
                if (n.getName().equals(na.getName())) {
                    speicher = n;
                }
            }
            // lösche die alte nachricht
            if (speicher != null) {
                int index = sadapter.nachrichtenliste.indexOf(speicher);
                sadapter.nachrichtenliste.remove(index);
            }
            synchronized (sadapter.nachrichtenliste) {
                this.sadapter.nachrichtenliste.add(na);
            }
    }



    public void melde_Abstract(String name, int Version, float wert){
        // hier synchronized rein???

            for (Input i : objektliste) {
                if (i.variablenName.equals(name)) {
                    // mache Versionscheck und schreibe dann dort den wert
                    synchronized (i) {
                        i.setAbstrakterWert(wert);
                    }

                }
            }
    }


When you use Handler.post() it will execute runnable on UI thread (if handler was created in UI thread). So when you do handler.post(this) you actually do all your communication on UI thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜