Java Multithreading

Concept of thread :

Consider the computer game, The person is moving from left to right. At the same time diamond shapes are blinking at the bottom.

Java Threading Example

Thread is used for dividing the task of an application into separate sub-processes which can run simultaneously.

Suppose we need to execute the program, then that program can be sub diveded into the number of small sub processes. Each small sub process is called as thread.

Introduction to Java Threads :

  1. A thread is a basic processing unit to which OS can allocate processor time.
  2. A thread is lightweight process.
  3. A program can have multiple threads.
  4. A thread consumes some resources so we should not use more thread than our requirement.
  5. A Java threads shares the common memory area so memory allocation for each thread is not required. So context switching requires less time than using the multi-processing.
  6. Every Java program has at least one thread i.e the thread that executes the Java Program
  7. Threads are faster and more efficient program which increases speed of execution
  8. Only one thread is executed at a time.

threading

Here is the snapshot of the complete OS. OS may contain the number of processes which shares the CPU time. A process again have multiple lightweight sub-processes called threads.

Understand Java Thread with Example :

Consider that we are opening the MS Power Point then -

  1. OS will start the process which would run MS Power Point.
  2. We can consider the MS Power Point as main process, in order to make interactive execution, MS Power Point process can create multiple sub processes i.e thread.
  3. When we type anything in the slide then spell checker thread can be automatically created. We can also consider the auto-correction as thread while typing.

Multithreading Vs Multiprocessing :

OS can start multiple processes to achieve multitasking. In order to make execution faster, each process can be sub divided into the smaller chunk of lightweight sub-processes which is called as thread.

Key difference :

  1. Multithreading refers to an application with multiple threads running within a process
  2. Multiprocessing refers to an application organised across multiple OS-level processes
Thread Process
Threads are easier to create Processes are not easier to create
Threads does not requires seperate address space Processrequires seperate address space
Threading should be done carefully as threads shares same data structures No need to worry because processes do not share common address space.
Threads can be considered as sub process so they are lightweight Processes are not lightweight.
Thread are not independent Processes can be independent
Thread cannot be divided into multiple processes Process can be divided into threads

Java Program has At least one Thread :

  1. Each and every java program have at least on thread
  2. First thread is created when you invoke the static main method of your Java class.
  3. Many java programs can have more than one thread which are created automatically.
  4. Thread is mostly used for game development but non-game applications can also use multi-threading