Threads in Java: How To Create Thread
Threads in Java: How To Create Thread
Threads in Java: How To Create Thread
Threads in Java
a) Java is a multi-threaded programming language which means we can develop
multi-threaded program using Java.
b) A multi-threaded program contains two or more parts that can run concur-
rently and each part can handle a different task at the same time making
optimal use of the available resources specially when your computer has mul-
tiple CPUs.
d) The OS divides processing time not only among different applications, but
also among each thread within an application.
1. Step 1
You will need to override run( ) method available in Thread class. This
method provides an entry point for the thread and you will put your
complete business logic inside this method. Following is a simple syntax
of run() method
1 public void run ( )
2. Step 2
Once the Thread object is created, you can start it by calling start()
method, which executes a call to run( ) method. Following is a simple
syntax of start() method
1 void start ( );
2
Runnable interface. This method provides an entry point for the thread
and you will put your complete business logic inside this method. Follow-
ing is a simple syntax of the run() method
public void run ()
2. Step 2
As a second step, you will instantiate a Thread object using the following
constructor
1 Thread ( Runnable threadObj , String threadName );
3. Step 3
Once a Thread object is created, you can start it by calling start() method,
which executes a call to run( ) method. Following is a simple syntax of
start() method
1 void start ();
}
31 }