Thread : 01 Basic
Multitasking
-
Multitasking
is a process of executing multiple tasks simultaneously.  
Use
multitasking to utilize the CPU.
Multitasking
has two types.
- process based multitasking
- thread based multitasking
Process
based multitasking ( multiprocessing )
*
each process has it is own address space in memory
*
each process allocates separate memory area.
*
heavy weight
*
Cost of communication between process is high
Thread
based multitasking ( multi-threading )
*
Threads share the same address space
*
light weight
*
Cost of communication between threads is low
Thread
: is a light weight sub
process. A smallest unit of processing.
Note:
There can be multiple processes inside the OS, and a process can have
1 or more threads.
Life
cycle of threads
- New : Create an instance of thread class
- Runnable : after invocation of start() method.
- Non-runnable ( blocked ) : thread is live, but not eligible to run.
- Terminated : thread is dead after run exit() rmethod.
 New
 → [ start() ] → Runnable
 Runnable
→ [ sleep, wait, wait for lock, block on I/O, suspend ] →
Non-runnable
 Non-runnable
→ [ sleep done, notify, lock available, I/O complete, resume ] →
Runnable
 Runnable
→ [ exit run() ] → Terminated
Create
a thread
- by extending Thread class
- by implementing runnable interface
Thread
class :
 Thread()
 Thread(String
name)
 Thread(Runnable
r)
 Thread(Runnable
r, String name)
Runnable
Interface
 public
void run() : is used to perform
action for a thread
Starting
a Thread
start()
method of the thread class is used to start newly created thread.
 *
a new thread starts
 *
threads moves from new state to runnable state
 *
when a thread get a chance to execute, it target run() method will
execute
Thread
Scheduler
*
Thread scheduler is a part of the JVM and it decides which thread
should runs.
*
There is no guarantee which runnable thread should get execute
*
Only one thread at a time will run in a single process
To
schedule a thread :
1. preemptive
scheduling – highest priority task execute until it
enters wait or dead state or very high priority task comes into
existence.
2. Time
slicing – tasks execute predefined slice of time and
then reenters to the pool.
Sleeping
a Thread
sleep()
method used to sleep a thread.
public
class
ThreadSample extends
Thread {
      public
void
run(){ 
            System.out.println("thread
is running.cx.." +
Thread.currentThread().getName());
            for(int
i=0; i<5; i++){
                  try{
                    Thread.sleep(500);
                }catch
(InterruptedException e) {
                    System.out.println(e);
                }
                System.out.println(i);
           }
      }
     /**
     * @param
args
     */
    public
static
void
main(String[] args) {
           ThreadSample
thread1 = new
ThreadSample();
          ThreadSample
thread2 = new
ThreadSample();
          ThreadSample
thread3 = new
ThreadSample();
          thread1.start();
           try{             
             thread1.join(3000);
          }catch
(Exception e) {} 
          thread2.start();
           thread3.start();
     }
}
Can
we start a thread twice ?
No.
After starting a thread, it can never been start again.
join()
method waits for a thread to die. 
