Ch.07 – Thread Per Message

Published by

on

Process vs Thread

  • Memory Share
    • Process has independent memory space while threads share memory.
    • As threads share memory with each other, communication between them are easier and this results in importance of exclusive control.
  • Context Switch
    • Normally, context switching is easier from threads than processes.

Example


Different ways of creating Threads

1. Using custom Thread class

2. Using anonymous class

3. Using Runnable

4. Using ThreadFactory

  • Main class that uses Host class can control the detail of Thread creation

5. Using ThreadFactory of Executors

  • Just need to update the ThreadFactory definition part from above case

6. Using Executor interface

  • void execute(Runnable r);
    • Executor interface has this method which can abstract what to run inside

7. Using ExecutorService interface

  • ExecutorService is a service which can repeatedly call execute
    • Unlike Executor which was generating new threads every time, ExecutorService can reuse the threads
  • AS ExecutorService has threads in the background, it has shutdown() method which would terminate them

Leave a comment