Last active
June 19, 2020 10:25
-
-
Save lunaspeed/c4e637fca15e8e4870aa2ea19380b8c3 to your computer and use it in GitHub Desktop.
multi-process sample
This file contains hidden or 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
func DoAndGetInt() (int, error) { | |
//do something for long time | |
return 1, nil | |
} | |
func DoAndGetString() (string, error) { | |
//do something for long time | |
return "1", nil | |
} | |
func main() { | |
var wg sync.WaitGroup | |
intChan := make(channel int, 1) | |
stringChan := make(channel string, 1) | |
errorChan := make(channel error, 2) | |
wg.Add(2) | |
go func() { | |
i, err := DoAndGetInt() | |
if err != nil { | |
errorChan <- err | |
} else { | |
intChan <- i | |
} | |
wg.Done() | |
}() | |
go func() { | |
s, err := DoAndGetString() | |
if err != nil { | |
errorChan <- err | |
} else { | |
stringChan <- s | |
} | |
wg.Done() | |
}() | |
wg.Wait() | |
close(errChan) | |
close(intChan) | |
close(stringChan) | |
var ( | |
myInt int | |
myString string | |
myError error | |
) | |
for myError = range errorChan { | |
} | |
if myError != nil { | |
//process error | |
} | |
for myInt = range intChan { | |
} | |
for myString = range stringChan { | |
} | |
//process successful result | |
} |
This file contains hidden or 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
//Before CompletableFuture | |
public static void main(String[] args) { | |
CountDownLatch cdl = new CountDownLatch(2); | |
Thread t1 = new Thread() { | |
public void run() throw Exception { | |
//do something | |
//Problem.....how do I return my answer? | |
cdl.countDown(); | |
} | |
}; | |
Thread t2 = new Thread() { | |
public void run() throw Exception { | |
//do something | |
//Problem.....how do I return my answer? | |
cdl.countDown(); | |
} | |
}; | |
cdl.await(); | |
//how to I get my answers from t1 and t2????? | |
} |
This file contains hidden or 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
function getExample() { | |
var a = promiseA(…); | |
var b = a.then(function(resultA) { | |
// some processing | |
return promiseB(…); | |
}); | |
return Promise.all([a, b]).then(function([resultA, resultB]) { | |
// more processing | |
return // something using both resultA and resultB | |
}) | |
.catch(function([errorA, errorB]){ | |
//handle my error | |
}); | |
} |
This file contains hidden or 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
def doAndGetInt(): Int = { | |
//do something for long time, may throw exception | |
1 | |
} | |
def doAndGetString(): String = { | |
//do something for long time, may throw exception | |
"1" | |
} | |
def main(args: Array[String]): Unit = { | |
val result = for { | |
i <- Future(doAndGetInt()) | |
s <- Future(doAndGetString()) | |
} { | |
//work with i and s | |
//will not get here if things fail | |
} | |
result match { | |
case Failure(e) => //work with error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment