Multithreading

Mulithreading:

  • Allow two or more parts of program execute concurrently
  • Threads are lightweight processes in process.
  • Creation of Thread
    • Extending Thread Class
    • Implementing runnable interface
  • Thread Class vs Runnable Interface
    • Class cannot extend another class if extending thread class (not multi inheritance)
    • Thread class have in built methods like yield and interrupt.
  • Synchronized blocks
    • Applied to an object of class
    • All thread applied synchronized on object enter one by one
  • Synchronise method or just some part under method

Inter-Thread Communication:

  • Polling : Test condition continuously until that return true and perform some action
  • To Tackle Pooling
    • wait() : It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify().
    • notify() : It wakes up one single thread that called wait() on the same object. It should be noted that calling notify() does not actually give up a lock on a resource.
    • notifyAll() : It wakes up all the threads that called wait() on the same object.

Producer-Consumer :

  • Multiprocess synchronization problem
  • Shares common size buffer as queue
  • Producer either sleep or discard data is buffer is full
  • Consumer consume data and notify Producer
  • Consumer go to sleep if buffer is empty
  • LinkedList as queue

CountDownLatch: Task wait for other threads to complete task

  • During creation have to specify no of threads to wait
  • Parse same reference to all threads and update counter of latch in every thread.

Thread Priority 0 to 10 Min_Priority(1) | Norm_priority(5) | Max_Priority(10)

Thread join pauses current thread execution till thread it joining finishes execution

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: