如何在 Android 中在第一个方法完全执行后执行第二个方法?

huangapple go评论78阅读模式
英文:

How to Execute second method after first method fully executed in Android?

问题

我有四个方法,它们必须依次执行。也就是说,第二个方法必须等到第一个方法完全执行完毕后才能执行。

在我的情况下,我在 IfElse 语句内部有一些方法,如下所示。它们的执行方式是第二个方法在第一个方法的某些部分执行后才执行。就像是异步方式一样。

if(flag.equalsIgnoreCase("flag2")) {
    method1();
} else if(flag.equalsIgnoreCase("flag3")) {
    method2();
} else if(flag.equalsIgnoreCase("flag4")) {
    method3();
}

setCommonData();
setRecyclerAdapter();

下面是我添加了同步关键字后的方法实现。

synchronized void method1() {
    Log.e("filter", "======");
    Class1 class1 = new Class1(this, GET, response -> {
        Log.e("response", "===success====");
    });
    NetworkManager.getInstance(this).sendRequest(class1);
}

其余的方法与 method 1 的实现相同。

我也尝试过使用 AsyncTasks,在 onBackgroundonPostExecute 中调用了两个方法。

我还尝试过使用线程 t.start()t.join()。但是同样会异步调用。

在我的情况下,我需要逐个执行这些方法。请在这个问题上给我一些帮助。

英文:

I have four methods, which have to execute one after another. So It means, second method have to wait until first method executes fully.

In my case I have methods inside my IfElse statements Like below. They are executing like second method is executing after some part execution in first method. Its like Asynchronous way.

if(flag.equalsIgnoreCase("flag2"))
{method1();
}else if(flag.equalsIgnoreCase("flag3")){
method2();
}else if(flag.equalsIgnoreCase("flag4"))
{ method3();
}

setCommonData();
setRecyclerAdapter();

and below is my method implementation after adding synchronization keyword.

 synchronized void method1()
    {
    Log.e("filter","======");
    Class 1 class1=new Class1(this,GET,response->{
    Log.e("response",""===success====);
    });
NetworkManager.getInstance(this).sendRequest(class1);
    }

Remaining methods are same as method 1 implementation.

I have used AsyncTasks Also by calling two methods inside onBackground and onPostExecute.

I have used Threads with t.start() and t.join();. But same those calling Asynchronously.
In my case I need to execute these methods one by one. Please some help me on this issue.

答案1

得分: 1

当你从不同的线程调用这些方法时,你可以在方法声明中添加synchronized关键字,以使method2method1返回之前等待:

synchronized void method1() { ...method1的代码... }
synchronized void method2() { ...method2的代码... }
synchronized void method3() { ...method3的代码... }

synchronized关键字使线程在当前对象的监视器上获取独占锁。如果另一个线程已经持有锁,新的调用将等待锁被释放。不使用synchronized关键字的方法不受影响,因为它们不需要锁。

一个可能更简单的解决方案是一开始就不使用不同的线程。如果你使用一个只有一个线程的线程池,自然而然地,一个方法调用必须等待前一个方法完成。但要向你展示如何做到这一点,你首先应该展示这些方法是如何被调用的。

英文:

When you're calling these methods from different threads, you can add the synchronized keyword to the method declaration to make method2 wait until method1 returns:

synchronized void method1() { ...code for method1... }
synchronized void method2() { ...code for method2... }
synchronized void method3() { ...code for method3... }

The synchronized keyword makes the thread acquire an exclusive lock on the current object's monitor. If another thread already holds the lock, the new call waits until the lock is free. Methods not using the synchronized keyword are not affected, since they don't need the lock.

A potentially simpler solution is to not have different threads in the first place. If you used a thread pool with only one thread, naturally one method call would have to wait for the previous to finish. But to show you how to do that, you should first show us how these methods get called in the first place.

答案2

得分: 0

你可能想要使用回调,将方法传递给彼此。

我在 P Burke 的帖子中链接了一个被接受的答案的 解决方案(它是用 JS 写的,但你仍然需要逻辑),它还可以指定调用的顺序。

英文:

You might wanna use callbacks, passing the methods to one another.

I'm linking a solution to an accepted answer on P Burke's post (it's in JS but you need the logic anyways) that can also specify the order of the calls.

huangapple
  • 本文由 发表于 2020年8月14日 18:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63411233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定