简体   繁体   English

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

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

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.就我而言,我的IfElse语句中有方法,如下所示。 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.其余方法同method 1实现。

I have used AsyncTasks Also by calling two methods inside onBackground and onPostExecute .我还通过调用onBackgroundonPostExecute内部的两个方法来使用 AsyncTasks。

I have used Threads with t.start() and t.join();我使用t.start()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.请在这个问题上帮助我。

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关键字,让 method2 等到 method1 返回:

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. synchronized关键字使线程在当前对象的监视器上获得排他锁。 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.不使用synchronized关键字的方法不受影响,因为它们不需要锁。

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.但是为了向您展示如何做到这一点,您应该首先向我们展示这些方法是如何被调用的。

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.我将解决方案链接到P Burke的帖子(它在 JS 中,但无论如何你需要逻辑)上的一个接受的答案,它还可以指定调用的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM