Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. # Threads
  2.  
  3. - A `thread` is a unit of execution
  4. - They run in parallel, different tasks associated with them
  5. - Efficient use of resources
  6. - Operating System divides processing time among the threads
  7. - Extend the thread class
  8. - Provides more flexibility
  9. - Methods
  10.  
  11. public void start() // invokes run on a thread object
  12. public void run() // When it is executing
  13. public final void setName(String name) // Give thread a name
  14. public void setPriority(int priority) // Gives thread priorities 1-10
  15. public final void setDaemon(boolean b)
  16. public final void join()// Current thread invokes this method on a second thread
  17. public final void interrupt() // Interrupts current thread
  18. public void isAlive() // Whether the thread is running or not
  19.  
  20. ### Priorities
  21.  
  22. - Cannot guarantee execution order
  23.  
  24. ### Life Cycle of Threads
  25.  
  26. - New Thread ⇒ New: start() ⇒ Runnable: run() ⇒ Running: sleep() / wait() ⇒ Waiting ⇒ Terminates / Dead
  27. - Running ⇒ Terminates
  28. - In waiting, a thread could be waiting for a resource other threads are using
  29. - Timed waiting
  30.  
  31. ### Threads Project
  32.  
  33. - Take large database
  34. - Create 8 threads; different functionalities
  35.  
  36. ### Sychronization
  37.  
  38. - Example:
  39. - A program that processes deposits and withdrawals
  40. - 2 Threads & 2 Variables
  41. - One variable is transaction name
  42. - One variable is amount
  43. - Uses monitors and locks
  44. - A `monitor` is a protective wrapper around code
  45. - Monitor will use `locks`
  46.  
  47. Struct A {
  48. A1(A a){
  49. int value;
  50. a.value = a.value-1;
  51. Queue<process> q
  52. if(a.value < 0){
  53. // log
  54. q.push(p);
  55. q.block();
  56. }
  57. if(a.value < 1){
  58. //logic
  59. a.pop();
  60. a.wakeup();
  61. }else{
  62. return;
  63. }
  64. }
  65.  
  66. A2(A1 a){
  67. a.value = a.value+1;
  68. if(a.value < 1){
  69. // logic
  70. q.pop();
  71. q.wakeup();
  72. }
  73. }
  74.  
  75. # Lock Interface
  76.  
  77. - This is better than synchronization
  78. - Guarantee of the sequence you are trying to produce
  79. - No timeout
  80. - Lock() and Unlock()
  81. - Can be called in different methods
  82. - Important Methods in Lock class
  83. - Lock()
  84. - Unlock()
  85. - public boolean TryLock()
  86. - Determine if you can put a lock on it; checks if lock is present or not
  87. - LockInterruptibility()
  88. - Some algorithms for traversing data structures that are concurrently accessed
  89. - "Hand over Hand"
  90. - Chain locking
  91. - Acquire ⇒ Release ⇒ Acquire next
  92. - Non blocking attempt to acquire a lock
  93. - TryLock()
  94.  
  95. - Attempt to acquire a lock that can be interrupted
  96. - LockInterruptibility
  97. - `Re-entrant Lock` enables many reads, but only one write at any given time
  98. - Can lock and unlock in different scopes
  99.  
  100. Lock l
  101. l.lock();
  102. try{
  103. // access that is protected by this lock
  104. }finally{
  105. // l.unlock();
  106. }
  107.  
  108. ### Starvation vs. Deadlock
  109.  
  110. - `Starvation` may happen if a process requires a resource for execution that is never allotted.
  111. - Faulty resource allocation decision
  112. - Low priority process may wait forever if a higher priority process monopolies what is needed
  113. - `Deadlock` happens when one thread holds a resource that the other one needs and vice versa
  114.  
  115. ### Fork / Join
  116.  
  117. - Both can break a task into smaller tasks for processing
  118. - Smaller tasks are independent
  119. - The `fork` part is the smaller broken up tasks
  120. - `Join` will take the results from the fork and combine them into one.
  121.  
  122. ### Condition Interface
  123.  
  124. - Provides a thread the ability to suspend it's new condition execution until a condition is true.
  125.  
  126. public void await() // Thread waits until a signal or interrupted
  127. public boolean await(long time, Time unit) // same as ^ or waiting until time elapsed
  128. public long await Interruptibility() // Thread waits until signalled
  129. public void signalled() // wake up a thread that is waiting
  130. public void signalAll() // wakes up all threads that are waiting
  131. public void awaitUntil() // Specified deadline has passed ll
  132.  
  133. ### Low Level Concurrency
  134.  
  135. - Includes
  136. - Synchronized
  137. - Can lead to performance issues
  138. - Volatile
  139. - Wait()T
  140. - Notify()
  141. - NotifyAll()
  142. - All those methods are not easy to use
  143. - Easy get deadlock, starvation, and brace conditions
  144.  
  145. ### High Level Concurrency
  146.  
  147. - Common choice for developers vs. low level
  148. - Use:
  149. - Semaphores
  150. - Thread pools
  151. - Specify the maximum amount of threads that the application will use
  152. - Executor Service
  153. - fixedPool = Executors new FixedThreadPool(int numOfThreads);
  154. - Maximum of numOfThreads will be active
  155. - If more than 2 threads are specified-extra threads put in a queue
  156. - `Executor` is any object capable of executing a task
  157. - Cannot be shutdown
  158. - Cannot be canceled
  159. - Exception methods in executor class:
  160. - boolean awaitTermination(long timeout, TimeUnit)
  161. - Block the calling thread until all the tasks have completed their execution
  162. - Shutdown request, timeout occurs, current thread is interrupted
  163. - boolean shutdown()
  164. - void shutdown()
  165. - orderly shutdown
  166.  
  167. # Mutual Exclusion
  168.  
  169. - No more than 1 thread can be in its `critical section` at any given time
  170. - A thread which dies in its critical non section will not affect other threads ability to continue
  171. - No deadlock
  172. - If a thread wants to enter critical section: it should be allowed to do so
  173. - No saturation
  174. - Threads not forced into a lock step execution of a critical section
  175.  
  176. # Dekker's Algorithm
  177.  
  178. - First solution of critical section problem
  179. - Many versions (up to 5)
  180. - 5th version - most efficient
  181. - Solution to critical section problem
  182. - Mutual exclusion, progress, bounded waiting
  183.  
  184. # Flavored Thread Motion
  185.  
  186. - Used to determine entry into critical section
  187. - Avoided deadlock
  188. - No indefinite postponement
  189.  
  190. main(){
  191. int flavoredThread = 1;
  192. boolean thread1WantsToEnter = false, thread2WantsToEnter = false;
  193. Thread1(){
  194. do{
  195. thread1WantstoEnter=true;
  196. while(thread2WantsToEnter){
  197. if(flavoredThread == 2){
  198. thread1WantsToEnter=false;
  199. while(flavoredThread==2){
  200. thread1WantsToEnter=true;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207.  
  208. $$S = \frac{1}{(1-p)(\frac{p}{s})}$$
  209.  
  210. - p = part improved, process
  211. - s = performance
  212. - p = 30%
  213. - s = double
  214.  
  215. # Synchronized vs. Re-entrant Lock
  216.  
  217. - `Synchronized` the implicit lock is given when you enter the block - automatic
  218. - Lock is released when thread leaves the block
  219. - `Re-entrant` Lock() and Unlock()
  220. - Synchronized is more rigid when multiple locks are acquired
  221. - Release is in opposite order
  222. - Re-entrant more flexible
  223. - Most functionality to a user
  224. - Fairness:
  225. - Synchronized: any waiting thread can acquire the lock
  226. - Can lead to stravationo
  227. - Re-entrant class has a constructor ⇒ boolean
  228. - Pass it as true, leads to threads waiting the longest are handled first
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement