1

I am developing on Android but the question might be just as valid on any other Java platform.

I have developed a multi-threaded app. Lets say I have a first class that needs to do a time-intensive task, thus this work is done in another Thread. When it's done that same Thread will return the time-intensive task result to another (3rd) class.

This last class will do something and return it's result to the first-starting class. I have noticed though that the first class will be waiting the whole time, maybe because this is some kind of loop ?

Also I'd like the Thread-class to stop itself, as in when it has passed it's result to the third class it should simply stop. The third class has to do it's work without being "encapsulated" in the second class (the Thread one). Anyone knows how to accomplish this ?

right now the experience is that the first one seems to be waiting (hanging) till the second and the third one are done :(

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
TiGer
  • 5,879
  • 6
  • 35
  • 36

4 Answers4

13

If you want to use threads rather than an AsyncTask you could 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 would kick it off by calling doBackgroundUpdate1(), when this is complete it sends a message to the handler which kicks off doBackgroundUpdate2() etc.

dave.c
  • 10,910
  • 5
  • 39
  • 62
2

Tiger ,

TiGer wrote:

When it's done that same Thread will return the time-intensive task result to another (3rd) class

Since thread runs asynchronously so your non-thread class can't be synced with your thread Though to perform some action on an Activity you need an AsyncTask not A Thread

TiGer wrote:

maybe because this is some kind of loop ?

Tiger do read more about Threads and concurrency

So the only answer I have for you now is ASYNCTASK

EDIT:

Also I'd like the Thread-class to stop itself

Read this post's how-do-you-kill-a-thread-in-java

Community
  • 1
  • 1
100rabh
  • 6,156
  • 5
  • 27
  • 41
  • thansk I have been working with AsyncTask till now but it doesn't suite my needs, as in it's more for a single task while having the option to update the GUI. I need a more object-orjented (complex) case, with several classses (and objects of those) interacting with each other and still having references to each other and also being able to update the GUI... Personally Handlers are better suited... – TiGer Jan 05 '11 at 09:50
  • TiGer You can invoke a method from your Thread & make that method call other class's methods & then use some global Handlers specified in your Application class to update the UI from any of the class – 100rabh Jan 05 '11 at 10:02
0

if you want use AsyncTask rather then thread in android I have resolve it using ASyncTask and Handler in Android the aim is that one task is execute after compilation of one task hear is code that show First load animation on view after compilation of that process it will goes on another page

 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);

    }
}
Irshad
  • 3,071
  • 5
  • 30
  • 51
Jayman Jani
  • 1,251
  • 10
  • 15
0

In ordinary Java, you would do this:

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");
    }
}

... that kicks off 10 threads. Notice that if you accidentally invoke t.run() instead of t.start(), your runnable executes in the main thread. Probably you'll see 'bye' printed before 10 'done'. Notice that the threads 'stop' when the the run() method of the Runnable you gave to them finishes.

I hope that helps you get your head around what it is you've got to co-ordinate.

The tricky part with concurrency is getting threads to communicate with each other or share access to objects.

I believe Android provides some mechanism for this in the form of the Handler which is described in the developer guide under designing for responsiveness.

An excellent book on the subject of concurrency in Java is Java Concurency in Practice.

David Bullock
  • 6,112
  • 3
  • 33
  • 43