Last active
December 14, 2015 08:19
-
-
Save zbigniewTomczak/5057190 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Concurrency - wielowatkowosc | |
--- | |
Keyboard/mouse response during program busy work | |
High level API - java.util.concurrent | |
Procesy i watki - processes and threads | |
Single core, time slicing | |
Multiple processors, multi core processors | |
IPC - inter process communication: pipes, sockets | |
Java create process - ProcessBuilder | |
Thread - lightweight process - watek - lekki proces | |
Watki wspoldziela zasoby procesu: pamiec i otwarte pliki. | |
Main thread. | |
Extend Thread or implement Runnable: public void run(), then start() | |
public static void sleep(long milis [, int nanos]) throws InterruptedException; | |
public void interrupt(); | |
public static boolean interrupted(); //czysci interrupted status | |
public boolean isInterrupted(); | |
public final void join() throws InterruptedException; | |
Memory consistency errors, happens-before, volatile | |
Synchronized methods, synchronized statements | |
Constructors cannot be synchronized | |
Wewntrzny zamek lub monitor obiektu - intrinsic lock, monitor lock | |
Watek moze zamknac, zwolnic lub posiadac lock | |
Reentrant synchronization - możliwoć wielokrotnego zakadania loków na tym samym obiekcie. | |
Operacja atomowa - może zostać wykonana w calosci lub w ogóle. Efekt czastkowy nie jest widoczny. | |
Zapis i odczyt jest atomowy dla wszystkich typów z wyjatkiem long i double i dla wszystkich zmiennych zadeklarowanych volatile. | |
Liveness: deadlock (dwie osoby chca strzelac z luku: pierwsza bierze luk, druga strzaly), starvation (jeden watek ma wyzszy priorytet, drugi nizszy), livelock (dwie osoby próbuja sie przepuscic w korytarzu). | |
Guarded block: | |
public synchronized guardedJoy() { | |
while(!joy) { | |
try { | |
wait(); | |
} catch (InterruptedException e) {} | |
} | |
} | |
Immutable objects are thread safe - obiekty niezmienne sa bezpieczne watkowo | |
1. brak setterow | |
2. pola finalne i prywatne | |
3. zabronic dziedziczenia: final class lub konstruktor prywatny | |
4. copy mutable fields on construction and before getters | |
java.util.concurrent.locks: Lock, Condition, ReadWriteLock | |
Executor: | |
void execute(Runnable command) | |
ExecutorService: | |
Future submit(Runnable task) | |
<T> Future<T> submit(Callable<T> task) | |
void shutdown() | |
List<Runnable> shutdownNow() | |
boolean awaitTermination(long timeout, TimeUnit unit) | |
ScheduledExecutorService: | |
schedule task with fixed delay | |
Thread pools: | |
Executors.newFixedThreadPool(int nThreads) | |
Executors.newCachedThreadPool() | |
Executors.newSingleThreadExecutor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment