<commit_before>#HSLIDE
# Java
lecture 10
## Practical Concurrency

#HSLIDE
## Отметьтесь на портале
https://atom.mail.ru/

#HSLIDE
### get ready
```bash
> git fetch upstream
> git checkout -b lecture10 upstream/lecture10
```
Refresh gradle project


#HSLIDE
## Agenda
1. Threads and processes
1. Multiple threads in game
1. Parallelism and Concurrency
1. What can go wrong with concurrency?
1. What to do?
1. Practice

#HSLIDE
## Agenda
1. **[Threads and processes]**
1. Multiple threads in game
1. Parallelism and Concurrency
1. What can go wrong with concurrency?
1. Synchronization. Critical section
1. Practice

#HSLIDE
## Parallelism
Why need parallelism?

#HSLIDE
## How parallelism is possible?
- A cluster of machines
- Several processes within single machine
- Several Threads within single process

#HSLIDE
### Process vs Thread
- **Process** has dedicated resources (memory)
- **Threads** share memory space

#HSLIDE
### Process vs Thread
<img src="lecture10/presentation/assets/img/process.png" alt="process" style="width: 450px;"/>

#HSLIDE
## Threads and processes. OS Role
Operating System
- provides API (**system calls**) for creating and running threads and processes. Normally a **program** is represented by single **process**.
- schedules threads and processes (**context switch**)
- provides API for managing threads

Famous Threads and Processes API from UNIX world:
- [**fork**](https://linux.die.net/man/2/fork) (creates process)  
- [**clone**](http://man7.org/linux/man-pages/man2/clone.2.html) (creates thread)  
  
#HSLIDE
## Java Threads
The programming language provide it's own API for managing threads and processes, that use system calls under the hood

In **Java** we get **Thread** class and **Runnable** interface

#HSLIDE
### interface Runnable
```java
@FunctionalInterface
interface Runnable {
    public abstract void run();
}
```


#HSLIDE
### class Thread
```java
class Thread implements Runnable {  
    void start() {}
    void interrupt() {} 
    void join() {}
    
    static sleep(long time) {}
    // ...
}
```


#HSLIDE
### Start and Run
```java
new Thread().start();

new Thread( runnable ).start();
```


#HSLIDE
### Start and Run
<img src="lecture10/presentation/assets/img/newthread.png" alt="exception" style="width: 750px;"/>

#HSLIDE
## Multithreaded programs are racy
Behaviour of multithreaded program is (inter alia) dependent on **OS scheduling**
Multithreaded programs are **racy** by nature.

#HSLIDE
## Race condition
Race condition (состояние гони, гонка)
program behaviour where the output is dependent on the
sequence or timing of other uncontrollable events
Parallel programs are racy by nature, some races are erroneous.
> @see ru.atom.lecture10.races

#HSLIDE
## jstack
Util to observe java process stack state.
 
```bash
# show all java processes
> jcmd
# get report
> jstack <pid>
```

#HSLIDE
## Agenda
1. Threads and processes
1. **[Multiple threads in game]**
1. Parallelism and Concurrency
1. What can go wrong with concurrency?
1. Synchronization. Critical section
1. Practice

#HSLIDE
## Game Server threads in Bomberman
<img src="lecture10/presentation/assets/img/GameThreads.png" alt="exception" style="width: 800px;"/>

#HSLIDE
## Multuple threads in Bomberman
Our Bomberman is a **client-server** game.  
Every player establishes **Session** with Server over **WebSocket**.  
Every connection is processed in **dedicated thread** (Actually thread from some **ThreadPool**).  
That is players are in different threads.

#HSLIDE
## How different threads communicate?
As usual - they can communicate via public variables, via mutable objects.<commit_msg>lecture10 presentation<commit_after>#HSLIDE
# Java
lecture 10
## Practical Concurrency

#HSLIDE
## Отметьтесь на портале
https://atom.mail.ru/

#HSLIDE
### get ready
```bash
> git fetch upstream
> git checkout -b lecture10 upstream/lecture10
```
Refresh gradle project


#HSLIDE
## Agenda
1. Threads and processes
1. Multiple threads in game
1. Parallelism and Concurrency
1. What can go wrong with concurrency?
1. What to do?
1. Practice

#HSLIDE
## Agenda
1. **[Threads and processes]**
1. Multiple threads in game
1. Parallelism and Concurrency
1. What can go wrong with concurrency?
1. Synchronization. Critical section
1. Practice

#HSLIDE
## Parallelism
Why need parallelism?

#HSLIDE
## How parallelism is possible?
- A cluster of machines
- Several processes within single machine
- Several Threads within single process

#HSLIDE
### Process vs Thread
- **Process** has dedicated resources (memory)
- **Threads** share memory space

#HSLIDE
### Process vs Thread
<img src="lecture10/presentation/assets/img/process.png" alt="process" style="width: 450px;"/>

#HSLIDE
## Threads and processes. OS Role
Operating System
- provides API (**system calls**) for creating and running threads and processes. Normally a **program** is represented by single **process**.
- schedules threads and processes (**context switch**)
- provides API for managing threads

Famous Threads and Processes API from UNIX world:
- [**fork**](https://linux.die.net/man/2/fork) (creates process)  
- [**clone**](http://man7.org/linux/man-pages/man2/clone.2.html) (creates thread)  
  
#HSLIDE
## Java Threads
The programming language provide it's own API for managing threads and processes, that use system calls under the hood

In **Java** we get **Thread** class and **Runnable** interface

#HSLIDE
### interface Runnable
```java
@FunctionalInterface
interface Runnable {
    public abstract void run();
}
```


#HSLIDE
### class Thread
```java
class Thread implements Runnable {  
    void start() {}
    void interrupt() {} 
    void join() {}
    
    static sleep(long time) {}
    // ...
}
```


#HSLIDE
### Start and Run
```java
new Thread().start();

new Thread( runnable ).start();
```


#HSLIDE
### Start and Run
<img src="lecture10/presentation/assets/img/newthread.png" alt="exception" style="width: 750px;"/>

#HSLIDE
## Multithreaded programs are racy
Behaviour of multithreaded program is (inter alia) dependent on **OS scheduling**
Multithreaded programs are **racy** by nature.

#HSLIDE
## Race condition
Race condition (состояние гони, гонка)
program behaviour where the output is dependent on the
sequence or timing of other uncontrollable events
Parallel programs are racy by nature, some races are erroneous.
> @see ru.atom.lecture10.races

#HSLIDE
## jstack
Util to observe java process stack state.
 
```bash
# show all java processes
> jcmd
# get report
> jstack <pid>
```

#HSLIDE
## Agenda
1. Threads and processes
1. **[Multiple threads in game]**
1. Parallelism and Concurrency
1. What can go wrong with concurrency?
1. Synchronization. Critical section
1. Practice

#HSLIDE
## Game Server threads in Bomberman
<img src="lecture10/presentation/assets/img/GameThreads.png" alt="exception" style="width: 850px;"/>

#HSLIDE
## Multuple threads in Bomberman
Our Bomberman is a **client-server** game.  
Every player establishes **Session** with Server over **WebSocket**.  
Every connection is processed in **dedicated thread** (Actually thread from some **ThreadPool**).  
That is players are in different threads.

#HSLIDE
## How different threads communicate?
As usual - they can communicate via public variables, via mutable objects.
{'fraction_non_alphanumeric': 0.11243986254295532, 'fraction_numerical': 0.012646048109965636, 'mean_word_length': 4.562691131498471, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1536221', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2162, 'n_words': 858}