31.07.
2008
Multi-Threading
- A light weight process within a process
- Each Java program is single threaded by default
- One thread is always present called as main thread
- Java provides a Thread class and Runnable interfaces for implementing the
multi-threading
- Runnable interfaces provide run() method to define the working of thread
- Threading can be implanted using two methods
o By inheriting Thread class
o By implementing the Runnable interface
Thread class
- To create and locate the threads
o Thread(Runnable r)
o Thread(Runnable r, String name)
o static Thread currentThread()
o String getName()
o void setName(String name)
o void start() – To add the thread in Thread queue
o void run() – To provide working to the thread
o static void sleep(int ms) throws InterruptedException
o static void sleep(int ms,int ns) throws InterruptedException
o void suspend() – To block state till resume() get called
o void resume() – To go back to ready state
o static void yield() – To provide control to CPU
Life Cycle of Thread
1. Born State
2. Ready State
3. Runnable State
4. Block State
5. Dead State
Priority
- Min Priority 1
- Max 10
- Default 5
- Methods
o setPriority(int n)
o int getPriority()