Multithreading in Java
Multithreading in Java:-
Two and more thread execute on same time is called a multi threading. When two thread working at a time then thread perform independently but its path is different. so at execution time if any exception caught in any one thread then that does not effect on other thread. Multi Threading is used for achieving a multi-taking.
Thread :-
Thread is a lightweight process. Thread is a basic
unit of CPU and which is perform its task independently because its path is different.
Thread is a pre defined class in Java which is belongs to the Java.lang
package.
Advantage of Multi-Threading :-
1.
Thread is
independent so it does not block the user.
2.
Multi
threading perform more than one task using at a time so it is saving the user
time.
3.
Multi
threading is increase the performance CPU.
4.
Decrease
cost of maintenance.
5.
Improvised
GUI responsiveness.
Disadvantage of Multi-Threading :-
1.
Complex Debugging
2.
complex
testing process
3.
Increase
potential for deadlock occurrence
4.
Unpredictable
result
5.
Increase
difficulty for writing a program.
Life cycle of Multi Threading:-
1.
New State: When a new thread is created that is a come at New
State. There is code is ready to run.
2. Runnable State: In this state thread is ready to move to running state at any time. Multi threading process is given a amount of time for a particular thread. When one thread is timeout after other thread at new state thread is ready to runnable state. It is not blocked and dead.
3.
Running
State: In this state thread is
running state by using of run() method there thread execution of it's action with given time slice by CPU.
4.
Waiting State: In this state thread is waiting some time then that
is waiting state. When a thread call and it is going to sleep mode or wait mode
by call sleep() or wait() method then thread move to moving state.
5.
Terminate: Thread is goes to terminate with some reason: First, Thread
is normally terminated it means code is successfully to completion. Second, if
any error is occurred then it is terminate.
Most Read:-Exception Handling in Java
Commanly methods used by threading:
- run(): run methid is used to perform a action for a thread.
- start(): starts method is used in thread for execution of thead then JVM call the run method to start the Thread.
- sleep(long miliseconds): sleep method is used in currently executing thread then thread goes sleep mode for some milliseconds.
- join(): When used join method in executing thread then a threads is waits for die.
- join(long miliseconds):When used join method in executing thread then a threads is waits for die for the specified milliseconds.
- suspend(): suspend method is used to suspend the thread.
- resume(): resume method is used to resume the suspended thread.
- stop(): stop method is used to stop the thread.
- getName(): getName method is used to returns the name of the thread.
- setName(String name): setName is used to change the name of the thread.
- currentThread(): currentThread method is used to return the reference of currently executing thread.
- getId(): getId method is used to return the id of the thread.
- getState(): getState method is used to return the state of the thread.
- isAlive(): isAlive method is used to test the thread, if the thread is alive. isAlive method return the boolean type value.
- interrupt(): interrupt method is used to Interrupt the thread.
Example of Threading:-
1. class is extends by Thread class :
0 Comments
Hello Friends, Please comment and share and do not write spam messages