You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.5 KiB
111 lines
2.5 KiB
6 years ago
|
/*
|
||
|
Landesamt für Geoinformation und Landentwicklung, Referat 35
|
||
|
Einführung in die Sprache Go, 25.7.2017
|
||
|
Lektion 7: Löse ein Problem objektorientiert
|
||
|
*/
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"math/rand"
|
||
|
"runtime"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type zeiten struct {
|
||
|
tt []int64
|
||
|
}
|
||
|
|
||
6 years ago
|
type zeitenMitEinheit struct {
|
||
|
unit string
|
||
|
zeiten // Vererbung
|
||
|
}
|
||
|
|
||
6 years ago
|
func (this *zeiten) zeitmess() { // Hier ist der Pointer notwendig, sonnst werden die Veränderungen nicht gespeichert. (Variablen global lesbar, lokal beschreibbar)
|
||
|
this.tt = append(this.tt, time.Now().UnixNano())
|
||
|
}
|
||
|
|
||
|
func (this zeiten) zeitgeb(index int) int64 {
|
||
|
return this.tt[index] - this.tt[index-1]
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
anzahlTöpfchen int8 = 25
|
||
|
anzahlKugeln int32 = 100000
|
||
|
)
|
||
|
|
||
|
var timestamp [2]int64
|
||
|
|
||
|
var wg, wg2 sync.WaitGroup // !
|
||
|
|
||
|
func main() {
|
||
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||
|
|
||
6 years ago
|
//var z zeiten = zeiten{}
|
||
|
var z zeitenMitEinheit = zeitenMitEinheit{unit: "Momentchen"}
|
||
6 years ago
|
var töpfchenAlle []int = make([]int, anzahlTöpfchen, anzahlTöpfchen)
|
||
|
var c chan bool = make(chan bool, 1) // !
|
||
|
|
||
|
z.zeitmess()
|
||
|
wg2.Add(1)
|
||
|
go func(t *[]int, ch chan bool) { // !
|
||
|
defer wg2.Done()
|
||
|
for true {
|
||
|
/* Bringt nichts, weil auf <-ch == true gewartet werden müßte:
|
||
|
time.Sleep(time.Second * 1)
|
||
|
if <-ch == true {
|
||
|
return
|
||
|
} else {
|
||
|
|
||
|
}*/
|
||
|
/*stattdessen:*/
|
||
|
select {
|
||
|
case <-ch: // oder <-ch == true
|
||
|
return
|
||
|
default:
|
||
|
fmt.Println("\u231B", *t, runtime.NumGoroutine())
|
||
|
time.Sleep(time.Second * 1)
|
||
|
}
|
||
|
}
|
||
|
}(&töpfchenAlle, c)
|
||
|
|
||
|
for k := (int32)(1); k <= anzahlKugeln; k++ {
|
||
6 years ago
|
wg.Add(1)
|
||
|
go zeigeMirDenGauß(int(anzahlKugeln), int(anzahlTöpfchen), &töpfchenAlle)
|
||
|
|
||
|
for runtime.NumGoroutine() >= 50 { // Limitiere die Anzahl der go-Routinen
|
||
6 years ago
|
time.Sleep(time.Millisecond * 1)
|
||
|
}
|
||
|
}
|
||
|
wg.Wait()
|
||
|
c <- true
|
||
|
|
||
|
wg2.Wait()
|
||
6 years ago
|
close(c)
|
||
6 years ago
|
|
||
|
fmt.Println("\u2690", töpfchenAlle)
|
||
|
z.zeitmess()
|
||
6 years ago
|
//fmt.Printf("\nProzess ist fertig und hat %v Sekunden gedauert!\n\n", (float64(z.zeitgeb(1)) * math.Pow(10, -9)))
|
||
|
fmt.Printf("\nProzess ist fertig und hat %v %s gedauert!\n\n", (float64(z.zeitgeb(1)) * math.Pow(10, -9)), z.unit)
|
||
6 years ago
|
}
|
||
|
|
||
|
func zeigeMirDenGauß(anzahlKugeln int, anzahlTöpfchen int, töpfchenAlle *[]int) {
|
||
|
defer wg.Done()
|
||
|
var töpfchenTmpZähler int
|
||
|
|
||
|
for i := 0; i < anzahlTöpfchen-1; i++ {
|
||
|
if yesnotrigger := func() bool {
|
||
|
if rand.New(rand.NewSource(time.Now().UnixNano())).Float64() < 0.5 {
|
||
|
return true
|
||
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
}(); yesnotrigger == true {
|
||
|
töpfchenTmpZähler++
|
||
|
}
|
||
|
}
|
||
|
(*töpfchenAlle)[töpfchenTmpZähler]++
|
||
|
}
|