0

I'm working on android. Please help give any advice. I have 3 methods(threads).

   Method_1 { trigger Method_3; ...}
   Method_2 { trigger Method_3; ...}
   Method_3 { ... }

How can I make sure that Method_3 only can be triggered by Method_1 or Method_2? In other words, if Method_1 is executed before Method_2, Method_2 will skip to trigger Method_3 if Method_3 is still running. Or, If Method_1 is executed before Method_2, Method_2 will trigger Method_3 if Method_3 which is triggered by Method_1 has been done. The same question, how do I do in 3 threads like this ...

   Thread_1 { trigger Thread_3; ...}
   Thread_2 { trigger Thread_3; ...}
   Thread_3 { ... }

Because Thread_3(Method_3) is related to some global socket variable. I want to guarantee if this thread/method is running, no one else can trigger it because it is trying to connect to the other end on the network. Thank you for your help!

Ray Sun
  • 25
  • 1
  • 7

2 Answers2

0

Use class variables to hold references to what method triggered what. Sounds like you need a reference to the method and a boolean state flag.

You can either use caching to store the result, preferences, or a global thats separated from the Activity, such as an async thread.

If the threads are managed from the same class, you can just use class variables that you store when destroyed in the savedInstance and load them back in onCreate.

Justin Mitchell
  • 665
  • 6
  • 13
0

Use class with global and static variable to handle threads. if thread 3 triggered update variable in class to avoid any thread trigger it. hope help you.

Moeen Kashisaz
  • 302
  • 1
  • 3
  • 13