Threads: multi-threading in Java

× "Is a" and "Has a" relation "Has a" relation Arrays as in memory Two Dimensional Arrays Exception Handling Variable, Object & Reference Multi-threading Website Design / Development Story of JEE modules
× JAVA, JEE, SQL: Online Private Tutoring Project based Training Concept based Training Basic Java:           $180 Advanced Java:     $300
(Threads,Sockets,JDBC,GUI)
Advanced Java:     $300
(with games projects like
PcMan, PingPong, TicTacToe)
Author: Bharat Chhajer

Overview

Thread: A thread is a line of execution of code.

Lets understand what is a line of execution of code. A Java program when executed has at least one thread called main thread. The main thread executes the code in the public static void main method of the java program. The execution of lines of code in the main method in a step by step way, by the main thread is in other words the line of execution of code.

JVM creates the main thread. The main thread executes the code in the main method(public static void main(String args[])). At the end of it the program exits. We may create more threads if we need. The threads that we create execute the code in the run method(public void run()). At the end of the run method the thread exits. There are two ways in which threads can be created in Java.

Using Thread Class
     Subclass the Thread class. Override the run method of the Thread class.
Using Runnable Interface
     Implement Runnable Interface in a class. The Runnable Interface has a run method, which is implemented in this class.

At times we need threads in Single Processor(with single core) machines too. Lets say that we are receiving a large stream of data, video and audio from the net and we don't want the GUI to look and feel frozen (GUI stops receiving mouse strokes), then we need a thread. Lets say that we are processing a large piece of data and we don't want the GUI to get frozen, then we need a thread.


Author: Bharat Chhajer