1

In my main method i have started 1 daemon thread which runs in background to check certain condition is satisfied, if satisfied then my main thread should wait for sometime and then continue.

Is it possible to do so? Controlling Main thread from another thread.

Actually am trying to automate 1 application where there are many pop-up windows displayed and I want to use 1 thread in background to check for pop-ups, if pop-ups are displayed then my main method should wait for some time then begin again.

MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
  • you want to change programs behaviour depending on daemon status? have you heard about `reflection`? I assume this is a good way to start with – Malakai Mar 20 '17 at 12:36
  • yes if certain condition is satisfied in daemon thread i just want to halt main thread for moment and begin again. am not aware of reflection. can you plz suggest any links? – mazhar uddin Mar 20 '17 at 12:42
  • 1
    CountDownLatches can work here..Possibly have a look here..http://stackoverflow.com/questions/289434/how-to-make-a-java-thread-wait-for-another-threads-output – Ankit Tripathi Mar 20 '17 at 12:46
  • Use a `static volatile boolean` that the thread sets and the main checks – Bohemian Mar 20 '17 at 12:55
  • As a general rule, you should never have one thread _make_ some other thread do something. Threads should cooperate with one another. If you want the main thread to pause when some condition is true, then the main thread should check the condition and pause itself. – Solomon Slow Mar 20 '17 at 13:57
  • @mazhar uddin Hope my answer was helpful to you, it talks about how you can communicate between two threads (in your case the main thread and the thread you create) using basic concepts of syncronization in java. If the answer was helpful and solved your purpose, you can accept the answer. http://stackoverflow.com/a/42906105/504133 – nits.kk Mar 21 '17 at 14:19
  • @nits.kk I Appreciate your effort, but my scenario is bit different. i have commented below on your answer. – mazhar uddin Mar 21 '17 at 17:23

1 Answers1

1

You can simply use the wait() and notify() on a common lock object.

From inside the main method, syncronize on the lock object. Within the synchronized block start your another thread and invoke wait() on the lock object.

In the run method of your second thread , write a synchronized block on the lock object and do your processing. Once it is done you can invoke the notify on the same lock object.

Main thread can then check if the required state has been set and then further actions can be decided (if you wish the main thread to complete its execution further or again wait and let second thread again do the processing) If you wish the second thread to again do (retry) the processing then like above you can invoke the notify() on the lock object and then can then invoke wait() on the same lock object.

This is the usual way of communication between two threads. But if its only single time process and you do not want it to happen multiple times then you can simply use the join() method. Main thread can join on the second thread. Till the second thread will be processing its task the main thread will be waiting for the processing to complete. Once the second thread is executed completely (end of run () method) control will reach the maim thread.

I suggest you to have a look at these methods. consumer-producer is a famous problem to understand these methods. You can also see these in action in an answer to another post. https://stackoverflow.com/a/42049397/504133

Community
  • 1
  • 1
nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • I understood what you are trying to explain, but my scenario is different. Am automating 1 application where many error pop-up may be displayed we don't know when that will be displayed or how many times it will be displayed. so what am trying to do is running a thread in background till main() completes its task, which will be checking for pop-up's if any in background. if background thread find any pop-up's i want my main() to wait and this background thread will deal with pop-ups. – mazhar uddin Mar 21 '17 at 17:19
  • Any suggestions?? am bit frustrated with this. – mazhar uddin Mar 25 '17 at 08:36