mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-08-03 13:12:16 +02:00
initial import
This commit is contained in:
25
concurrency/best-practices-generator-function/main.go
Normal file
25
concurrency/best-practices-generator-function/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func printOut() <-chan string {
|
||||
c := make(chan string)
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := printOut()
|
||||
for i := 1; i < 10; i++ {
|
||||
fmt.Println(<-c)
|
||||
}
|
||||
}
|
42
concurrency/best-practices-multiplexer-select/main.go
Normal file
42
concurrency/best-practices-multiplexer-select/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func printOut() <-chan string {
|
||||
c := make(chan string)
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func join(in1, in2 <-chan string) <-chan string {
|
||||
out := make(chan string)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case t := <-in1:
|
||||
out <- t
|
||||
case t := <-in2:
|
||||
out <- t
|
||||
}
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
c1 := printOut()
|
||||
c2 := printOut()
|
||||
c3 := join(c1, c2)
|
||||
for i := 1; i < 10; i++ {
|
||||
fmt.Println(<-c3)
|
||||
}
|
||||
}
|
42
concurrency/best-practices-multiplexer/main.go
Normal file
42
concurrency/best-practices-multiplexer/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func printOut() <-chan string {
|
||||
c := make(chan string)
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func join(in1, in2 <-chan string) <-chan string {
|
||||
out := make(chan string)
|
||||
go func() {
|
||||
for {
|
||||
out <- <-in1
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
out <- <-in2
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
c1 := printOut()
|
||||
c2 := printOut()
|
||||
c3 := join(c1, c2)
|
||||
for i := 1; i < 10; i++ {
|
||||
fmt.Println(<-c3)
|
||||
}
|
||||
}
|
54
concurrency/best-practices-sync/main.go
Normal file
54
concurrency/best-practices-sync/main.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
text string
|
||||
wait chan interface{}
|
||||
}
|
||||
|
||||
func printOut() <-chan Message {
|
||||
finish := make(chan interface{})
|
||||
c := make(chan Message)
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
c <- Message{fmt.Sprintf("Print %v", i), finish}
|
||||
time.Sleep(time.Duration(rand.Intn(9000)) * time.Millisecond)
|
||||
<-finish
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func join(in1, in2 <-chan Message) <-chan Message {
|
||||
out := make(chan Message)
|
||||
go func() {
|
||||
for {
|
||||
out <- <-in1
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
out <- <-in2
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
c1 := printOut()
|
||||
c2 := printOut()
|
||||
c3 := join(c1, c2)
|
||||
for i := 1; i < 20; i++ {
|
||||
message1 := <-c3
|
||||
message2 := <-c3
|
||||
fmt.Println(message1.text)
|
||||
fmt.Println(message2.text)
|
||||
message1.wait <- ""
|
||||
message2.wait <- ""
|
||||
}
|
||||
}
|
49
concurrency/best-practices-timeout/main.go
Normal file
49
concurrency/best-practices-timeout/main.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func printOut() <-chan string {
|
||||
c := make(chan string)
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func join(in1, in2 <-chan string) <-chan string {
|
||||
out := make(chan string)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case t := <-in1:
|
||||
out <- t
|
||||
case t := <-in2:
|
||||
out <- t
|
||||
}
|
||||
}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
c1 := printOut()
|
||||
c2 := printOut()
|
||||
c3 := join(c1, c2)
|
||||
|
||||
timeout := time.After(3 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case t := <-c3:
|
||||
fmt.Println(t)
|
||||
case <-timeout:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
31
concurrency/channel-mit-close/main.go
Normal file
31
concurrency/channel-mit-close/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var c chan string
|
||||
|
||||
func printOut() {
|
||||
for i := 1; i < 10; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
close(c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
c = make(chan string)
|
||||
go printOut()
|
||||
|
||||
for {
|
||||
t, open := <-c
|
||||
if open {
|
||||
fmt.Println(t)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
25
concurrency/channel-range/main.go
Normal file
25
concurrency/channel-range/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var c chan string
|
||||
|
||||
func printOut() {
|
||||
for i := 1; i < 10; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
close(c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
c = make(chan string)
|
||||
go printOut()
|
||||
for t := range c {
|
||||
fmt.Println(t)
|
||||
}
|
||||
}
|
17
concurrency/channels/main.go
Normal file
17
concurrency/channels/main.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
c := make(chan string)
|
||||
|
||||
go func() {
|
||||
fmt.Println(<-c)
|
||||
}()
|
||||
|
||||
c <- "Hello World!"
|
||||
|
||||
close(c)
|
||||
|
||||
}
|
24
concurrency/first-go-routine-mit-channel/main.go
Normal file
24
concurrency/first-go-routine-mit-channel/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var c chan string
|
||||
|
||||
func printOut() {
|
||||
for i := 1; ; i++ {
|
||||
c <- fmt.Sprintf("Print %v", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
c = make(chan string)
|
||||
go printOut()
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(<-c)
|
||||
}
|
||||
}
|
19
concurrency/first-go-routine/main.go
Normal file
19
concurrency/first-go-routine/main.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func printOut() {
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Printf("Print %v\n", i)
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
go printOut()
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
3
concurrency/graceful-shutdown/.vscode/settings.json
vendored
Normal file
3
concurrency/graceful-shutdown/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"go.formatTool": "goimports"
|
||||
}
|
3
concurrency/graceful-shutdown/go.mod
Normal file
3
concurrency/graceful-shutdown/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module graceful-shutdown
|
||||
|
||||
go 1.12
|
60
concurrency/graceful-shutdown/main.go
Normal file
60
concurrency/graceful-shutdown/main.go
Normal file
@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
)
|
||||
|
||||
func handle(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
select {
|
||||
case <-time.After(60 * time.Second):
|
||||
// If we receive a message after 2 seconds
|
||||
// that means the request has been processed
|
||||
// We then write this as the response
|
||||
w.Write([]byte("request processed"))
|
||||
case <-r.Context().Done():
|
||||
// If the request gets cancelled, log it
|
||||
// to STDERR
|
||||
fmt.Fprint(os.Stderr, "request cancelled\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
srv := http.Server{Addr: ":8081"}
|
||||
|
||||
idleConnsClosed := make(chan struct{})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
sigint := make(chan os.Signal, 1)
|
||||
signal.Notify(sigint, os.Interrupt)
|
||||
<-sigint
|
||||
// We received an interrupt signal, shut down.
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
// Error from closing listeners, or context timeout:
|
||||
log.Printf("HTTP server Shutdown: %v", err)
|
||||
}
|
||||
log.Print("HTTP server Shutdown successful")
|
||||
close(idleConnsClosed)
|
||||
}()
|
||||
|
||||
http.HandleFunc("/", handle)
|
||||
|
||||
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
||||
// Error starting or closing listener:
|
||||
log.Printf("HTTP server ListenAndServe: %v", err)
|
||||
}
|
||||
|
||||
<-idleConnsClosed
|
||||
|
||||
}
|
40
concurrency/java-threads-beispiel/Main.java
Normal file
40
concurrency/java-threads-beispiel/Main.java
Normal file
@ -0,0 +1,40 @@
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class Main {
|
||||
|
||||
static int count = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
count++;
|
||||
try {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(120000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Thread Hello " + count);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
System.out.println("create native thread "+ count);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user