Multi Threading - Introduction

Overview

In this part we are gonna learn the following topics:

  1. Introduction
  2. They ways to define a Thread
    1. By Extending java.lang.Threads class
    2. By Implementing java.lang.Runnable class
  3. Getting & Setting name of Threads
  4. Thread Priorities
  5. Methods to prevent Threads execution
    1. yield()
    2. join()
    3. sleep()
  6. Thread Synchronization
  7. Inter Thread Communication
  8. Deadlock
  9. Daemon Threads
  10. Multi Threading Enhancements

Introduction

Multi Tasking: 

Multi Tasking is doing multiple tasks simultaneously. In our daily PC usage most often we write on some editor and listen to audio songs which is an example of multi tasking. Multi tasking is basically of the following two types:
  1. Process Based: Executing several tasks simultaneously where each tasks in a separate independent program is called Process Based multi tasking. Process based multi tasking is an Operating System level multi tasking. 
    1. E.g: Multiple programs running on a computer have single process each and they can run simultaneously, it's an example of process based multi tasking. So if you say to your customer that "You can listen to audio songs, download files from the internet while using the application software I've created for you". That doesn't mean that your program supports multi tasking, because it's an OS level multi tasking.
  2. Thread Based: Executing several tasks simultaneously where each tasks is a separate part of the same program is called Thread Based multi tasking, here each independent part of a program is called a thread. It's a program level multi tasking.


To create an application program that responses faster, utilizes better computer resources we do multi threading. Multi threading have greater implementation is the following areas:
  • Multi Media Graphics
  • Video Games
  • Animations
  • Web/Application Server
Story-1: In our modern Televisions we see lot's of even't occurs simultaneously. For example in a scenario of a movie birds are flying, human dancing, airplane is floating & sun is shining. All we're observing simultaneously in a moment's. Now imaging if your system was unable to do multi tasking then how many hours it would take to see the scenario? It would show all the events one after another. Here multi tasking eased our job.

Story-2: If we wanna make a program that will search for "untitled" named folders from out computer, how it'll search them up? It'll search the following hierarchy of folders using BSF/DSF or any other searching algorithms:


If our program doesn't support multi tasking then it'll take 40-50 hours to complete the searching procedure.
In comparison to old languages, developing multi threaded application in java is very easy. Because java provide in built support with lot of API Thread.java class, Runnable interface etc.



Comments