Multithreading is a process in which two or more threads ( a lightweight sub process ) execute simultaneously. Each part of a program is called thread.

Firstly a program comes in new state , then it goes in runnable state. If we give “run” command on runnable state then it will go to running state, at running state if we give “yield” command then the program return to the runnable state. For blocking the thread from runnable state we can use suspend, sleep or wait. If we use “stop” method at any state then our program will go to the “dead” state.
Blocking a thread
- Sleep - Sleep method block a thread for a specified time. Ex. Sleep(time in millisecond)
- Suspend - it block the thread until the further order. The thread can be revived by “resume” method.
- Wait - wait block the thread until the certain condition occurred. The thread can be revived by using “notify” method.
Sample of thread program:
- Class A extends Thread
- {
- Public void run()
- {
- for(int i=0;i<=5;i++)
- {
- System.out.println(“from thread A value of i =”+i);
- }
- System.out.println(“exit from thread A”);
- }
- }
- Class B extends Thread
- {
- Public void run()
- {
- for(int j=0;j<=5;j++)
- {
- System.out.println(“from thread B value of j =”+j);
- }
- System.out.println(“exit from thread B”);
- }
- }
- Class C extends Thread
- {
- Public void run()
- {
- for(int k=0;k<=5;k++)
- {
- System.out.println(“from thread C value of k =”+k);
- }
- System.out.println(“exit from thread C”);
- }
- }
- Class Thread test
- {
- Main ()
- {
- new A().start ();
- new B().start ();
- new C().start ();
- }
- }

Join the conversation! Your thoughts help the community grow.