Thread is a path of execution of a program. It can be called as single unit of execution in a program. A program can have more than one thread. Every program has at least one thread. Threads are used to do multiple activities simultaneously in a process. For example:- If you want to type something and show date and time simultaneously you can use threads. MS Word works on the principle of threads so that you can modify the file and print it simultaneously. Threads are called light weight processes.
Every java program has atleast one thread the main thread. When a java program starts, jvm creates the main thread and calls the program's main method. Additional thread is created by extending the Thread class.
Threads have three stages in its life. Start, run, stop. Threads can be started using start method, the start method automatically calls run method to make it running. To end the life cycle of the thread stop method is called or thread shows an exception or error or run method comes to an end.
In the above program 2 thread classes are created AlphabetPrint and NumberPrinter. The objects of the two classes is created in class AlphaNumericPrint and the two threads are started. When both are started the run() in each thread is automatically called and the lines in the corresponding run()s are executed. In the run method of NumberPrinter, print() is called which displays numbers from 65 to 89 and in the run() of AlphabetPrint calls print() of AlphabetPrint and displays the equivalent characters of integers 1 to 25.
Here the two processes displaying alphabets and numbers are done simultaneously.