Skip to content

Instantly share code, notes, and snippets.

View AlexeySoshin's full-sized avatar

Alexey Soshin AlexeySoshin

View GitHub Profile
// There's no proper UUID package still, so we'll deal with floats
m := make(map[float64]float64)
// This will allow us to wait for all goroutines to complete
doneChannel := make(chan bool, 10)
// Launch goroutines
for i := 0; i < 10; i++ {
go func() {
// Generate some random numbers
m := make(map[float64]float64)
// Create mutex manually
mux := sync.Mutex{}
doneChannel := make(chan bool, 10)
for i := 0; i < 10; i++ {
go func() {
for i := 0; i < 1000; i++ {
ExecutorService executor = Executors.newFixedThreadPool(4);
for (int i = 0; i < 10; i++) {
executor.execute(() -> {
//No Java without boilerplate!
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
System.out.println("Finished");
});
}
for i := 0; i < 10; i++ {
go func() {
time.Sleep(1 * time.Second)
fmt.Println("Finished")
}()
}
@AlexeySoshin
AlexeySoshin / waitgroup_broken.go
Last active September 3, 2017 16:33
Concurrency in Go: WaitGroup
wg := sync.WaitGroup{}
// Wait for all child goroutines
wg.Add(10)
for i := 0; i < 10; i++ {
go func(wg sync.WaitGroup) {
time.Sleep(1 * time.Second)
fmt.Println("Finished")
wg.Done()
}(wg)
}
@AlexeySoshin
AlexeySoshin / fixed_waitgroup.go
Last active September 3, 2017 16:32
Concurrency in Go: Fixing WaitGroup
wg := &sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func(wg *sync.WaitGroup) {
time.Sleep(1 * time.Second)
fmt.Println("Finished")
wg.Done()
}(wg)
}
wg.Wait()
@AlexeySoshin
AlexeySoshin / waitgroup_closure.go
Last active September 3, 2017 16:32
Concurrency in Go: WaitGroup with closure
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
time.Sleep(1 * time.Second)
fmt.Println("Finished")
wg.Done()
}()
}
wg.Wait()
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
word := `Maximilianstraße`
@AlexeySoshin
AlexeySoshin / KotlinSpringBootApplication.kt
Created October 22, 2017 16:38
All you need to init SpringBoot with Kotlin
@SpringBootApplication
class KotlinSpringBootApplication
fun main(args: Array<String>) {
// That part of boilerplate you still cannot get rid of
SpringApplication.run(KotlinSpringBootApplication::class.java, *args)
}
// No need for setters getters for a simple POJO
data class Cat(var id : UUID? = null,
val name: String,
val age: Float,
val weight: Float,
var sex : Cat.Sex = Sex.Unknown) {
enum class Sex {
Unknown,
Male,
Female,