related link
John Nilsson
john at milsson.nu
Tue Feb 9 13:52:04 PST 2010
On Tue, Feb 9, 2010 at 10:05 PM, Neal Gafter <neal at gafter.com> wrote:
> How would you do this using the macro scheme?
>
Given the macro I previously defined. The following would be expanded as
below. But you were right, it was quite instructive. What does it mean to
return concurrently? I guess the right thing to do is to change the macro to
block the first thread waiting for the futures and return from there.
public Collection<Principal> getAttendees() {
List<Principal> result = new ArrayList<Principal>();
forEachConcurrently(EventResponse r : getResponses(), threadPool) {
if (r.mayAttend()) {
Principal attendee = r.getAttendee();
synchronized (result) {
result.add(attendee);
}
}
}
return result;
}
public Collection<Principal> getAttendees() {
List<Principal> result = new ArrayList<Principal>();
final Collection<Callable<Object>> tasks = new
ArrayList<Callable<Object>>();
final Collection<Future<Object>> futures = new
ArrayList<Future<Object>();
for(final EventResponse t : getResponses())
{
tasks.add(Executors.callable(new Runnable(){public void run() {
EventResponse r = t; //Slight bug in the macro, should be 'T
retT' not just 'retT'
forEachConcurrently_1234: {
if (r.mayAttend()) {
Principal attendee = r.getAttendee();
synchronized (result) {
result.add(attendee);
}
}
}
}}));
}
try {
futures.addAll(threadPool.invokeAll(tasks));
} catch (InterruptedException ex) {}
return result;
}
BR,
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20100209/ec0c12bd/attachment-0001.html
More information about the closures-dev
mailing list