简体   繁体   中英

What happens when- A. Activity method is called from Runnable B. Multiple threads call Activity method simultaneously

For the first part of the question, please consider the following pseudocode.

public class Main extends Activity {
    .
    .
    // all necessary overridden methods
    .
    .
    Button.onClick() {
        (new Task()).run();
    }

    public void myMethod() {
        // do something here
    }

    public class Task extends Runnable {
        @Override
        public void run() {
            myMethod();
        }
    }
}

I am no expert in Java or any other programming language, so I'm unable to understand how the method myMethod() will be executed when the Button is clicked. Will myMethod() execute inside the separate Thread of the Runnable as a separate "instance" of the method? Or will it execute inside the UI Thread since it is a part of my Activity class.

Now for the second part of the question, please consider the following pseudocode.

public class Main extends Activity {
    .
    .
    // all necessary overridden methods
    .
    .
    Button.onClick() {
        (new TaskOne()).run();
        (new TaskTwo()).run();
    }

    public void myMethod() {
        // do something here
    }

    public class TaskOne extends Runnable {
        @Override
        public void run() {
            for(int i=0; i<20; i++) {
                myMethod();
                Thread.sleep(2000);
            }
        }
    }
    public class TaskTwo extends Runnable {
        @Override
        public void run() {
            for(int i=0; i<20; i++) {
                myMethod();
                Thread.sleep(2000);
            }
        }
    }
}

If myMethod() runs in the UI Thread due to it being a part of the Activity , what will happen when the method is called simultaneously by the two Runnable instances? I'm so confused. Will each Runnable instance execute a separate "instance" of the method, and if not, what will happen? Will each thread wait for each other's execution of myMethod() ?

My confusion arises from trying to implement LoaderManager.LoaderCallbacks . What if onLoadFinished() is called while onLoadFinished() is already running due to successive database updates?

I hope this post is appropriate to be on StackOverflow. If it is not, I'll promptly remove it. Thanks for any help. I really appreciate it.

1) It will run in a separate thread (not UI Thread);

2) There will be a race condition. myMethod() will be called by both threads at the same time. To prevent possible errors you will have to make myMethod() synchronized or add synchronized block inside the method on certain objects.

All of these happens or course if you start new threads correctly :) now you call run() on Runnable which is not the right way. You should use (new Thread(new TaskOne())).start(); .

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