threads

24
Multithreading in Java Multithreading in java is a process of executing multiple threads simultaneously. • Thread is basically a lightweight sub- process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. • But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. • Java Multithreading is mostly used in games, animation etc

Upload: mallikarjun

Post on 17-Aug-2015

215 views

Category:

Documents


1 download

DESCRIPTION

hh

TRANSCRIPT

Multithreading in JavaMultithreading in javais a process of executing multiple threads simultaneously.Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.ut we use multithreading than multiprocessing because threads share a common memory area. They don!t allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.Java Multithreading is mostly used in games, animation etc"dvantage of Java Multithreading#tdoesn't block the userbecause threads are independent and you can perform multiple operations at same time.$% &oucan perform many operations together so it saves time.'% Threads areindependentso it doesn!t a(ect other threads if exception occur in a single thread.MultitaskingMultitasking is a process of executing multiple tasks simultaneously. )e use multitasking to utili*e the +,-. Multitasking can be achieved by two ways.,rocess-based Multitasking/Multiprocessing%Thread-based Multitasking/Multithreading%,rocess-based Multitasking /Multiprocessing%0ach process have its own address in memory i.e. each process allocates separate memory area.,rocess is heavyweight.+ost of communication between the process is high.1witching from one process to another re2uire some time for saving and loading registers, memory maps, updating lists etc.Thread-based Multitasking /Multithreading%Threads share the same address space.Thread is lightweight.+ost of communication between the thread is low.)hat is Thread in 3ava4" thread is a lightweight sub process, a smallest unit of processing. #t is a separate path of execution.Threads are independent, if there occurs exception in one thread, it doesn!t a(ect other threads. #t shares a common memory area.5ife cycle of a Thread /Thread 1tates%" thread can be in one of the 6ve states. "ccording to sun, there is only 7 states inthread life cycle in javanew, runnable, non-runnable and terminated. There is no running state.ut for better understanding the threads, we are explaining it in the 8 states.The life cycle of the thread in 3ava is controlled by J9M. The 3ava thread states are as follows.:ew;unnable;unning:on-;unnable /locked%Terminated