0

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.

basha
  • 587
  • 2
  • 6
  • 25
  • Possibly covered by "[Android AsyncTask wait until finished](//stackoverflow.com/q/34535066/90527)", "[Android - How do I continuously run a thread, one after another](//stackoverflow.com/q/27820764/90527)", "[Wait for an asynchronous task in asynctask in android](//stackoverflow.com/q/34433957/90527)", "[How does synchronized work in Java](https://stackoverflow.com/q/749641/90527)" – outis Jan 20 '22 at 23:40
  • [Code samples](//stackoverflow.com/help/mcve) should be minimal, *complete* and representative. – outis Jan 20 '22 at 23:41

2 Answers2

1

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • We need to see more of your code, or you need to prepare a [minimal working example](https://stackoverflow.com/help/minimal-reproducible-example). For example, what class defines these methods, and how does the code get called. The `synchronized` key word prevents concurrent access to methods of one object. It sounds like you may actually have *two* objects of the same class? – Joni Aug 14 '20 at 17:20
  • I have added method implmentation in my question. Please check. – basha Aug 18 '20 at 12:55
  • That's still not enough, to answer the question you need to know what Class1 and NetworkManager are and what their methods do – Joni Aug 18 '20 at 15:48
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • if OP is asking about a question in java, there's no point in linking to a post in another language or framework, that definitely doesn't help ! i have written [a guide on callbacks](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value) which might be of more value – a_local_nobody Aug 14 '20 at 11:39
  • While you are right, I could argue that his problem isn't entirely java-specific but a "how to achieve something" to which i provided a method of doing so. Moreover, by providing my asnwer, someone with a more accurate answer appeared, so, in the end, we all benefit! Finally, I checked your reply! Good job on also including a Kotlin answer! – Filippos Georgiou Aug 14 '20 at 12:02