Application/Multithreading

Multithreading-Programming means that a program contains two or more parts that can run cuncurrently and handle different tasks at the same time. IS especially true if Computer contains several CPUs.
Stages:
* New
* Runnable
* Running
* Waiting
* Dead
Once you create it, it is new. once you start it, it is runnable, once it is in run, it's running, if you use sleep or wait, it's waiting, following the execution, it is dead. 
== Difference Runnable and Thread ==
Thread has more methods, e.g. set- and getName
interface vs. abstract class (-> Interface can implement more, class can only be extended from one)
also has priorities
== Kill a Thread ==
Cannot be done from outside -> You must signal a thread to do it yourself
* boolean flag as a global variable -> while(flag) -> set it to true
* Thread.currentThread().isInterrupted() -> while(!Thread.currentThread().isInterrupted()) -> <Thread>.interrupt();
== Volatile ==
volatile variables: variables that are not cached in the CPU, they are written and read from the main memory
Danger: While multithreating, a thread might only work with a cached copy of the variable -> CPU does eventually write (altered) value back to the main-memory, but you can't guarantee when exactely this will be -> Inconsistency if multiple threads have to work on it simultaniously 
guarantees thread safety when:
* one thread writes and one only reads
* all to shared value with atomic functions
== Synchronized ==
a function is locked when accessed by a thread and only released when the thread is done with it. next thread can only access it once it is released
-> guarantees Thread safety when volatile can not
Can also be performed on resources (synchronize(<Object>)), or plainly code (Synchronize(<this>))