Skip to content

Instantly share code, notes, and snippets.

@mtov
Last active November 14, 2019 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtov/a3f73350010758925172a7f433e2ba71 to your computer and use it in GitHub Desktop.
Save mtov/a3f73350010758925172a7f433e2ba71 to your computer and use it in GitHub Desktop.
Exemplo de testabilidade - Chamada Assíncrona
class TaskResult {
double value;
public synchronized void set(double value) {
this.value = value;
}
public double get() {
return value;
}
}
public class MyMath {
public double syncPI (int prec) {
return 3.1415926;
}
public void asyncPI(int prec, TaskResult task) {
new Thread (new Runnable() {
public void run() {
double pi = syncPI(prec); // compute PI
task.set(pi);
}
}).start();
}
public static void main (String [] args) {
MyMath m = new MyMath();
TaskResult task = new TaskResult();
m.asyncPI(10, task);
try { Thread.sleep (1000); } catch (InterruptedException ex) {}
System.out.println(task.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment