Method performance when waiting for data to received

Hello,

I have the following scenario and solution, i’d like to know whether my solution is good enough and efficient or bad implementation.

I Have to wait for SMS confirmation code(the code is 4 digits). when it’s received I need to type it manually without using “sendKeys” method because the code is changed each time.
my issue was suspending the test and wait on the relevant screen until I enter the code(Manually) and then let the test continue automatically.

The solution I though about is the follow:
within the use case:

  1. wait until switching to the relevant screen. i do that using :

    _waitForAction.until(ExpectedConditions.visibilityOf(wd.findElement(By.className(“UIATextField”) )));`
    I’m waiting until the verification code text box appear on the screen

2.next ,I have created a Thread lets say “waitForSMS” which contains a while loop that checks if the textBox contains text in length of 4 (implementation is below)
3. I have created an Object lets say gotSMS.
4. within Synchronized Block I start the tread and gotSMS.wait()

synchronized (gotSMS) {
			waitForSMS.start();
                        System.out.println("Thread start");
			System.out.println("wait start");
			gotSMS.wait();
			System.out.println("done waiting");
		}

5.when I entered the code it immediately stops the while loop within the thread and calls gotSMS.notify()
as follow:

Thread waitForSMS = new Thread(new Runnable() {

  	@Override
  	public void run() {
  		WebElement e; codeLength = 4;
  		e = wd.findElement(By.className("UIATextField"));
  		e.sendKeys("0"); //avoid getting the default text which is "Enter Verification Code"
  		while((e = wd.findElement(By.className("UIATextField"))).getText().length() < codeLength){
  			System.out.println("text: " + e.getText() +" le:" + e.getText().length());
  		}
  		System.out.println("code is ok notify");
  		synchronized (gotSMS) {
  			gotSMS.notify();
  			System.out.println("notified");
  		}
  		System.out.println("wait done");
  	}
  });

it works, pretty well, What do you think about the While loop within the Thread? is findElement efficient ?

you have a better solution??

another question,
how do you use Appium when you have to wait for Data to received from server(Query to DB, etc…)?? is it ok to use Threads ,wait,notify??

Thanks.