Skip to content

Commit

Permalink
Prepare for concurrent lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Nov 18, 2023
1 parent 0987b32 commit 1171976
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/07-Concurrent-Programming.slide
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ What will be the result?

.play ../src/concurrent/waitgroup/parallel_for_loop.go /func/,

* Excercise

This is the previous code for atomic counting.

.play ../src/concurrent/atomic/atomic.go /var/,

Use a waitgroup to wait for all threads to finish before printing the final result.

* Go Channels
.image img/06-go-concurrency.jpeg
Expand Down
4 changes: 2 additions & 2 deletions docs/exercises/Exercise6.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ main = do secret <- getRandomFromTime

```

#### Excercise 6.6b - Determine status
#### Excercise 6.5b - Determine status

Given the main function

Expand All @@ -109,6 +109,6 @@ The function should have the following signature:
status :: Int -> Int -> IO Bool
```

#### Excercise 6.6c - Loop
#### Excercise 6.5c - Loop

Finally implement a loop until the user has guessed the correct number.
38 changes: 33 additions & 5 deletions docs/exercises/Exercise7.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,34 @@

If you do not finish during the lecture period, please finish it as homework.

## Exercise 7.1 - Generator
## Exercise 7.1 - Waitgroup

The following code starts 10000 threads that increment a counter.

```go
var myYoutubevideo struct {
likes int32
}

func Viewer() {
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
atomic.AddInt32(&myYoutubevideo.likes, 1)
}

func main() {
for i := 0; i < 10000; i++ {
go Viewer()
}
for i := 0; i < 8; i++ {
fmt.Println(myYoutubevideo.likes, "likes")
time.Sleep(200 * time.Millisecond)
}
}
```

Use a waitgroup to wait for all threads to finish before printing the final result.

## Exercise 7.2 - Generator

Write a generator for Fibonacci numbers, i.e. a function that returns a channel where the next Fibonacci number can be read.
```go
Expand All @@ -14,7 +41,7 @@ func main() {
}
```

## Exercise 7.2 - Timeout
## Exercise 7.3 - Timeout

Write a function `setTimeout()` that times out an operation after a given amount of time. Hint: Have a look at the built-in `time.After()` function and make use of the `select` statement.
```go
Expand All @@ -32,11 +59,12 @@ func main() {
}
```

## Exercise 7.3 - Dining Philosophers
## Exercise 7.4 - Dining Philosophers

Write a program to simulate the Dining Philosophers Problem by using Go Channels.
Write a program to simulate the [Dining Philosophers](https://en.wikipedia.org/wiki/Dining_philosophers_problem)
Problem by using Go Channels.
- There should be one Go Routine for each Philosopher
- The table itself should be a Go Routine and should handle all forks. This makes synchronization easier.
- The table itself should be a single Go Routine and should handle all forks via channels and a single select statement. This makes synchronization easier.
- Make sure that:
- The distribution of forks is fair - No philosopher dies on starvation
- Use the given Unit Test:
Expand Down
31 changes: 31 additions & 0 deletions src/concurrent/atomic/atomic_waitgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"math/rand"
"sync"
"sync/atomic"
"time"
)

var myYoutubevideo struct {
likes int32
}

func Viewer() {
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
atomic.AddInt32(&myYoutubevideo.likes, 1)
}

func main() {
wg := sync.WaitGroup{}
for i := 0; i < 10000; i++ {
wg.Add(1)
go func() {
Viewer()
wg.Done()
}()
}
wg.Wait()
fmt.Println(myYoutubevideo.likes, "likes")
}
2 changes: 2 additions & 0 deletions src/concurrent/java/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
java threads.java

32 changes: 32 additions & 0 deletions src/concurrent/java/threads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
int numberOfTasks = 1000;
int threadPoolSize = 1000;

ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);

for (int i = 0; i < numberOfTasks; i++) {
executorService.submit(() -> {
try {
Thread.sleep(10000); // Wait for 3 seconds
System.out.println("Thread " + Thread.currentThread().getId() + " finished waiting.");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}

executorService.shutdown();

try {
// Optional: Wait for all tasks to finish execution
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
18 changes: 18 additions & 0 deletions src/haskell/intToBinary.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- - Implement a converter function of an integer to a binary string.
--
-- ```Haskell
-- intToBinary :: Int -> String
-- ....
--
-- main = print $ intTooBinary 42 -- returns "101010"
-- ```

intToBinary :: Int -> String
intToBinary 0 = "0"
intToBinary n = reverse (binary n)
where
binary 0 = ""
binary n = show (n `mod` 2) ++ binary (n `div` 2)

main = do
print $ intToBinary 5
35 changes: 35 additions & 0 deletions src/servers/tcp/interactive/interactive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"bufio"
"io"
"net"
"os"
)

func main() {
conn, err := net.Dial("tcp", "localhost:8081")
if err != nil {
panic(err)
}
go func() {
reader := bufio.NewReader(os.Stdin)
for {
input, _, err := reader.ReadRune()
if err != nil {
panic(err)
}
_, err = conn.Write([]byte(string(input)))

if err != nil {
panic(err)
}
}
}()
for {
_, err := io.Copy(os.Stdout, conn)
if err != nil {
panic(err)
}
}
}

0 comments on commit 1171976

Please sign in to comment.