Skip to content

Instantly share code, notes, and snippets.

@mtov mtov/MyMath.java
Last active Nov 14, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.