Showing posts with label anonymous. Show all posts
Showing posts with label anonymous. Show all posts

2013-08-30

Java: Simple multithreading a piece of code

import java.util.*;
import java.util.concurrent.*;

// ...

ExecutorService exec = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());
try {
    for (int i = 0; i < num_of_runs; i++) {
        exec.submit(new Runnable() {
            @Override
            public void run() {
                // do stuff in num_of_runs times through a 2*#CPU threadpool
            }
        });
    }
} finally {
    exec.shutdown();
}

2013-08-29

C#: Anonymous delegates asynchronously

this.BeginInvoke(new Action(delegate
{ 
 MessageBox.Show("Asynchronous :D"); 
}));

System.Action is a framework-defined Delegate successor, that does not return anything and doesn't take any arguments. You should use this, instead of fucking around with inheriting from System.Delegate.