Difference between Multithreading and Multiprocessing



Multiprocessing:

ü  To obtain maximum throughput

ü  To reduce average execution time of process


A Thread is a single sequence of execution that can run independently in an application.
Multithreaded programs support more than one concurrent thread of execution

Multithreading represents concurrent execution of multiple threads. Multithreading is a lightweight version of multiprocessing i.e. less over head is incurred by the o.s. in multithreading as compare to multiprocessing.

Difference between Multithreading and Multiprocessing

ü  In multiprocessing each process represents an independent application where as in multithreading each thread represents an independent module of an application.

ü  Each process as it own addresses space where as all the threads in an application share a common address space.

Difference between Thread and Process:

ü  Thread share address space of the process that created it; processes have their own address space.

ü  Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.

ü  Change to the main thread (cancellation, priority change) may affect the behavior of the other threads of the process; change to the parent process does not affect child process.

A Thread is represents by an instance of java.lang.Thread class. This class provides various methods to obtain the state of a thread, to change the state of thread, to request the execution of the thread, to suspend the execution of the thread etc. All methods in thread class except one are final.

“A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon.”

“When java program startup, one thread begins running immediately. This is usually called the main Thread of your program, because it is the one that is executed when your program begins.”

Main thread is important for two reasons:

    It is thread from which other “child” threads will be spawned.
    Often it must be the last thread to finish execution because it performs various shutdown actions.

Although the main thread is created automatically when your program is started, it can be controlled through a Thread object. To do so, you must obtain a reference to it by calling the method currentThread(), which is public static member of Thread.

public class MainThreadTest {

public static void main(String as[])throws Exception{

    Thread th=Thread.currentThread();

    System.out.println(th.getName());

    System.out.println(th.getPriority());

    Thread.sleep(1000);

    th.setName("Main Thread");

    th.setPriority(7);

    System.out.println(th.getName());

    System.out.println(th.getPriority());

}

}

Out put:

main

5

Main Thread

7

No comments:

Post a Comment