Multithreaded Programming

  • A Multithreaded Program contains tow or more programs that can run concurrently.
  • Each part of such program is called thread. which defines its own path of execution.
  • Thus Multithreading is the specialized form of multi-tasking.

Multitasking

  • It is the feature which is virtually supported by all operating systems.
  • Two types of multi tasking are:
Process-based multitasking
  • A process in, is essence, a program that is executing.

  • This type allows our computer to run two or more programs at a same time.

Ex: When running a java program we can use web browser or editing files etc.

Thread-based multitasking

  • A thread is the smallest unit of dispatchable code means a program can perform two or more tasks simultaneously.

Ex: A text editor can format text at the same time that it is printing.

Multitasking Processes Multitasking Threads
This required more overhead when compared to multitasking threads. This requires less overhead
Processes are heavy weight task that required their own separate address space. These are the lighter weight.
Inter process communication is expensive and limited. Inter thread communication is in expensive.
context switching from one process to another also costly. context switching from one thread to another also lower in cost.
Java programs makes use if process based multitasking but it is not under java control. This is under java control.

Multi-threading

  • It enables us to write efficient programs that make maximum use of the processing power available in the system.
  • This is done by making the idle time to minimum because another thread can run when one is waiting.
  • Ex:user input is slower than computer, data rate from network is slower than computer speed.

The Java Thread Model

  • Java uses threads to enable the entire environment to be asynchronous. This helps reduce inefficiency by preventing the waste of CPU cycles.
  • In a single-threaded model which follows event loop with polling environment, when a thread blocks (that is, suspends execution) because it is waiting for some resource, the entire program stops running.
  • The benefit of Java’s multithreading is that One thread can pause without stopping other parts of your program. which is done by allowing the other threads to run while one is blocked which takes more time than idle time.

States of Thread

  1. Running-Thread which is currently running.
  2. Ready- As soon as the thread gets CPU time to run then it will be in ready state to run it.
  3. Suspended-Temporarily halts the activity of the thread.
  4. Resumed-The thread which is suspended can be resumed to continue its task.
  5. Blocked-When the thread is waiting for a resource.
  6. Terminated-At any time thread can be terminated which halts its execution completely halt which can not be resumed.

Thread Priorities

  • These are the integers that specify the relative priority of one thread to another.
  • This is used to decide when to switch from one running thread to the next. This is called a context switch.
  • The rules while performing context switch are

A thread can voluntarily relinquish control-In this all threads are examined, and the highest-priority thread that is ready to run is given the CPU.

A thread can be preempted by a higher-priority thread-In this case, a lower-priority thread that does not yield the processor is simply preempted—no matter what it is doing— by a higher-priority thread. Basically, as soon as a higher-priority thread wants to run, it does. This is called preemptive multitasking.

Synchronization

  • A multi-threading uses asynchronous behavior there will be some situations that two threads may need data from same data structure, That is we have to prevent one thread from writing data while another thread is in the middle of reading it.
  • which is done by synchronization.

The monitor:A monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.

each object has its own implicit monitor that is automatically entered when one of the object’s synchronized methods is called.

Messaging

  • This is a system where java provides a two or more threads to talk to each other, via calls to predefined methods that all objects have.
  • It allows to enter in to a synchronized method and wait until some other thread explicitly notifies it to come out.

The Thread Class and the Runnable Interface

The java 's multi threading system depends on the Thread class and the Runnable interface where we can create a Thread by extending the Thread class or by implementing the Runnable interface.

TheThreadsclass defines several methods as follows

getName()- get thread name,

getPriority()- get threads priority,

isAlive()-determine is thread is in running state or not

join()-wait for a thread to terminate.

run()- entry point for thread,

sleep()-to suspend state for a period of time.

start()-start a thread by calling it's run() method.

results matching ""

    No results matching ""