


non-determinism = parallel processing + mutable state
Non-determinism caused by concurrent threads accessing shared mutable state.




Paralelismo por capa de procesamiento - Asynchronous and Partial Results From Request/Response to Request Stream/Response Stream

Cómo hacer uso de la manera más eficiente los recursos multicore, GPUs, Clusters? Cómo manejar eventos asincronicos? Cómo responder frente a los delays y errores?
Objects are characterized by state, identity and behavior
public class DummyThread extends Thread implements
AccessPoint {
public void startListening(Properties props,
String interpreterName) {
this.setDaemon(false);
this.setName("GarbageCollectorInvoker");
this.start();
}
public void stopListening() {
stop=true;
synchronized (this){
notifyAll();
}
}
public boolean isListening() {
return !stop;
}
}
public void run() {
int interval = 0;
try{
while(!stop){
try{
synchronized (this){
wait(interval);
System.gc();
}
}catch(Exception e){
log.error(e.getMessage());
}
}
}
public class SocketEventHandler extends Handler {
public SocketEventHandler(SocketEventHandlersManager mgr){
super(mgr);
}
public void run(){
try {
Object message = null;
EventHandlersManager manager = (EventHandlersManager) getManager();
while (!stopProcessing){
changeState(Handler.States.WAITING);
message = manager.getNextMessageToSend();
if (message!=null){
this.sendMessageToClient(message);
changeState(Handler.States.WORKING);
}
}
}catch (InterruptedException e) {
}catch (Exception e) {
}finally{
changeState(Handler.States.DEAD);
}
}
}
public class Person {
public final String name;
public final int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
}
class Person(val name: String, val age: int)
import java.util.ArrayList;
Person[] people;
Person[] minors;
Person[] adults;
{
ArrayList minorsList = new ArrayList();
ArrayList adultsList = new ArrayList();
for (int i = 0; i < people.length; i++)
(people[i].age < 18 ? minorsList : adultsList).add(people[i]);
minors = minorsList.toArray(people);
adults = adultsList.toArray(people);
}
val people: Array[Person]
val (minors, adults) = people partition (_.age < 18)
val people: Array[Person]
val (minors, adults) = people.par partition (_.age < 18)
class Rational(n: Int, d: Int) extends Ordered[Rational] {
def compare(that: Rational) =
(this.numer * that.denom) - (that.numer * this.denom)
require(d != 0)
private val g = gcd(n.abs, d.abs)
val numer = n / g
val denom = d / g
def this(n: Int) = this(n, 1)
def + (that: Rational): Rational =
new Rational(
numer * that.denom + that.numer * denom,
denom * that.denom
)
def + (i: Int): Rational =
new Rational(numer + i * denom, denom)
def - (that: Rational): Rational =
new Rational(
numer * that.denom - that.numer * denom,
denom * that.denom
)
def - (i: Int): Rational =
new Rational(numer - i * denom, denom)
def * (that: Rational): Rational =
new Rational(numer * that.numer, denom * that.denom)
def * (i: Int): Rational =
new Rational(numer * i, denom)
def / (that: Rational): Rational =
new Rational(numer * that.denom, denom * that.numer)
def / (i: Int): Rational =
new Rational(numer, denom * i)
override def toString = numer +"/"+ denom
private def gcd(a: Int, b: Int): Int =
if (b == 0) a else gcd(b, a % b)
override def equals(other: Any): Boolean =
other match {
case that: Rational =>
(that canEqual this) &&
numer == that.numer &&
denom == that.denom
case _ => false
}
def canEqual(other: Any): Boolean =
other.isInstanceOf[Rational]
override def hashCode: Int =
41 * (
41 + numer
) + denom
}
object Main {
def main(args: Array[String]) {
val x = new Rational(2, 3)
println("x [" + x + "]")
println("x * x [" + (x * x) + "]")
println("x * 2 [" + (x * 2) + "]")
implicit def intToRational(x: Int) = new Rational(x)
val r = new Rational(2,3)
println("2 * r [" + (2 * r) + "]")
}
}
import scala.actors._
object FooActor extends Actor {
def act(){
for(i <- 1 to 11){
doSomething()
Thread.sleep(1000)
}
}
}
val echoActor = actor {
while(true) {
receive {
case msg => println("Received message " + msg)
}
}
}
scala> echoActor ! "My First Message"
(defn fac-cps [n k]
(letfn [(cont [v] (k (* v n)))]
(if (zero? n)
(k 1)
(recur (dec n) cont))))
(defn factorial [n]
(fac-cps n identity))
public class StringUtils {
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false {
return false;
}
}
return true;
}
}
(defn blank? [str]
(every #((Character/isWhitespace %) str))
(defrecord Person [name age])
(def foo (->Person "Alex" 34))
(defrecord Message [sender text])
(def messages (ref ()))
(def validate-message-list
(partial every? #(and (:sender %) (:text %))))
(def messages (ref () :validator validate-message-list))
(defn naive-add-message [msg]
(dosync (ref-set messages (cons msg @messages))))
(defn add-message [msg]
(dosync (alter messages conj msg)))
(defn add-message-commute [msg]
(dosync (commute messages conj msg)))
(ns sample (:require [http.async.client :as http]))
(with-open [client (http/create-client)]
(let [response (http/GET client "http://neotyk.github.com/http.async.client/")]
(-> response
http/await
http/string)))
(map (fn [x] (.toUpperCase x)) (.split "Dasher Dancer Prancer" " "))
(reduce + (r/map inc [1 2 3])) === (reduce (fn [ret x] (+ ret (inc x))) (+) [1 2 3])
fn * reducible -> reducible