Multitasking and Multithreading:


refers to a computer's ability to perform multiple jobs concurrently
more than one program are running concurrently, e.g., UNIX


A thread is a single sequence of execution within a program
refers to multiple threads of control within a single program
each program can run multiple threads of control within it, e.g., Web Browser

Concurrency vs. Parallelism:

Threads and Processes:

Use Of Threads:

  • To maintain responsiveness of an application during a long running task
  • To enable cancellation of separable tasks
  • Some problems are intrinsically parallel
  • To monitor status of some resource (e.g., DB)
  • Some APIs and systems demand it (e.g., Swing)
Note: so we can say that thread is basically used in gaming, animation , multimedia, software development.

Application Thread:

1)When we execute an application:

  • The JVM creates a Thread object whose task is defined by the main() method
  • The JVM starts the thread
  • The thread executes the statements of the program one by one
  • After executing all the statements, the method returns and the thread dies


  • Each thread has its private run-time stack
  • If two threads execute the same method, each will have its own copy of the local variables the methods uses
  • However, all threads see the same dynamic memory, i.e., heap (are there variables on the heap?)
  • Two different threads can act on the same object and same static fields concurrently
Creating Threads:

  • There are two ways to create our own Thread object
  • Subclassing the Thread class and instantiating a new object of that class
  • Implementing the Runnable interface
  • In both cases the run() method should be implemented

By Thread Class:

public class ThreadTest extends Thread {
   public void run () {
      for (int i = 1; i <= 100; i++) {

Threads Method:

void start()

  • Creates a new thread and makes it runnable
  • This method can be called only once

void run()

  • The new thread begins its life inside this method

void stop() (deprecated)

  • The thread is being terminated

void yield()

  • Causes the currently executing thread object to temporarily pause and allow other threads to execute
  • Allow only threads of the same priority to run

void sleep(int m) or sleep(int m, int n)  

  • The thread sleeps for m milliseconds, plus n nanoseconds.

By Implementing Runnable:

public class RunnableTest implements Runnable {
                public void run () {
                                for (int i = 1; i <= 100; i++) {
                                System.out.println (“***”);


When running the Runnable object, a Thread object is created from the Runnable object
The Thread object’s run() method calls the Runnable object’s run() method
Allows threads to run inside any object, regardless of inheritance

Starting Thread:

public class ThreadsStart
       public static void main (String argv[])
         ThreadTest tt=new ThreadTest();
         Runnable Test rt=new RunnableTest();
         Thread t=new Thread(rt);
         tt.start ();//Using thread class
         t.start();//using runnable interface

Thread State:

 Thread Working :

Daemon Threads:

  • Daemon threads are “background” threads, that provide services to other threads, e.g., the garbage collection thread
  • The Java VM will not exit if non-Daemon threads are executing
  • The Java VM will exit if only Daemon threads are executing
  • Daemon threads die when the Java VM exits.
  • Main is also a Daemon Thread.

Thread Scheduling:

  • Thread scheduling is the mechanism used to determine how runnable threads are allocated CPU time.
  • A thread-scheduling mechanism is either preemptive or non-preemptive.
  • Threads are scheduled according to their priority w.r.t. other threads in the ready queue.
  • The highest priority runnable thread is always selected for execution above lower priority threads.
  • When multiple threads have equally high priorities, only one of those threads is guaranteed to be executing.
  • Java threads are guaranteed to be preemptive-but not time sliced

Preemptive scheduling :

  • The thread scheduler preempts (pauses) a running thread to allow different threads to execute.

Nonpreemptive scheduling:

  • The scheduler never interrupts a running thread.
  • The non-preemptive scheduler relies on the running thread to yield control of the CPU so that other threads may execute.

Time-sliced scheduling:

  • The scheduler allocates a period of time that each thread can use the CPU,when that amount of time has elapsed, the scheduler preempts the thread and switches to a different thread.

Nontime-sliced scheduler:

  • the scheduler does not use elapsed time to determine when to preempt a thread it uses other criteria such as priority or I/O status.

Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mobile: 9953584548
Write us at: - and

No comments:

Post a Comment