Problems with MultiThreading when programming for Android

I am developing Android, but the question may be just as correct on any other Java platform.

I developed a multithreaded application. Suppose I have a first class that must perform a laborious task, so this work is done in a different thread. When this is done, the same thread will return the result of setting the time to another (third) class.

This last class will do something and return the result to the first class. I noticed that the first class will wait all the time, maybe because it's some kind of cycle?

In addition, I would like the Thread class to stop, because in the case when it passed the result, the third class should just stop. The third class should work without being "encapsulated" in the second class (Thread). Does anyone know how to do this?

right now the experience is that the first seems to be waiting (hanging) until the second and third: (

+1
source share
4 answers

If you want to use streams rather than AsyncTask, you can do something like this:

private static final int STEP_ONE_COMPLETE = 0;
private static final int STEP_TWO_COMPLETE = 1;

...

private doBackgroundUpdate1(){
    Thread backgroundThread = new Thread() {
        @Override
        public void run() {
            // do first step

            // finished first step
            Message msg = Message.obtain();
            msg.what = STEP_ONE_COMPLETE;
            handler.sendMessage(msg);
        }
    }
    backgroundThread.start();
}
private doBackgroundUpdate2(){
    Thread backgroundThread = new Thread() {
        @Override
        public void run() {
            // do second step

            // finished second step
            Message msg = Message.obtain();
            msg.what = STEP_TWO_COMPLETE;
            handler.sendMessage(msg);
        }
    }
    backgroundThread.start();
}
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
        case STEP_ONE_COMPLETE:
            doBackgroundUpdate2();
            break;
        case STEP_TWO_COMPLETE:
            // do final steps;
            break;
        }
    }
}

You could start it by calling doBackgroundUpdate1()when it is complete, it will send a message to handler, which starts doBackgroundUpdate2(), etc.

+11
source

Tiger,

TiGer wrote:

, , ()

, non-thread - Activity AsyncTask A Thread

TiGer ():

, ?

Tiger do Threads concurrency

, , , - ASYNCTASK

EDIT:

Thread

how-do-you-kill-a-thread-in-java

+2

Java :

class MyTask implements Runnable {
    void run() {
       for (int i = 0; i < Integer.MAX; i++) {
          if (i = Integer.MAX -1) {
              System.out.println("done");
          }
       }
    }
}


class MyMain {
    public static void main(String[] argv) {
        for (int i = 0; i < 10; i++) {
           Thread t = new Thread(new MyTask());
           t.start();
        }
        System.out.println("bye");
    }
}

..., 10 . : t.run() t.start(), runnable . , "", 10 "". , " run() Runnable, , .

, , .

concurrency .

, Android Handler, .

concurrency Java - Java Concurency .

0

AsyncTask, android AsyncTask Handler Android, , , , ,

 class gotoparent extends AsyncTask<String,String,String>

{
    @Override
    protected String doInBackground(String... params) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {



                        Animation animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotete);
                        lin2.startAnimation(animation);




            }
        });

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent i=new Intent(getApplicationContext(),ParentsCornor.class);
                startActivity(i);

            }
        }, 1200);

    }
}
0

All Articles