initial import

This commit is contained in:
SourceFellows
2020-08-21 06:26:40 +02:00
commit e223458dd4
423 changed files with 9871 additions and 0 deletions

View 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)
}
}

View 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)
}
}

View 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)
}
}

View 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 <- ""
}
}

View 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
}
}
}

View 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
}
}
}

View 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)
}
}

View 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)
}

View 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)
}
}

View 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)
}

View File

@ -0,0 +1,3 @@
{
"go.formatTool": "goimports"
}

View File

@ -0,0 +1,3 @@
module graceful-shutdown
go 1.12

View 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
}

View 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();
}
}