Last active
November 14, 2019 17:47
-
-
Save mtov/a3f73350010758925172a7f433e2ba71 to your computer and use it in GitHub Desktop.
Exemplo de testabilidade - Chamada Assíncrona
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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