Java Multithreading. If you want to get knowledge and
clear understanding about use of multithreading in Java. Then
its a best place for you, here i will teach you in detail about Java
multithreading with very easy to understand examples.
Java Multithreading
What is Thread
In Java programming, we can have more than one part of a single
program and when we execute two or more than two parts of a single program so
to get the full power of CPU. When we execute different part of a single program
at once, these different parts are known as thread in Java language. It is
important to know that threads are light weight process because they are part
of a big program
What is Multithread
Whenever we execute more than one thread at the same time, this is
known as Multithreading in Java. The practical implementation of multithreading
are Games. Threads have shared memory location. And when there is some
exception occurs in one thread, it doesn’t affect the other threads.
When we execute more than one thread, we get lots of benefits like
threads are independent, user task is not blocked by them, they are useful to
save lots of time etc
How to create Thread in Java
To create threads in Java, we have two ways
1. By extending Thread class
2. By implement runnable interface
Thread class
To create a thread using thread class, first we have to create our own class that will extends the thread class. It is important because we need very important method for thread which is known as run method and we can get this method by extending the thread class.
When we created our class and extends the Thread class, after that we have to create object of our class and to actually start the execution of thread, we have to call the start() method.
Example of Thread using Thread Class
class A extends Thread
{
public void run()
{
try
{
System.out.println
(“I am thread”);
}
catch(Exception
obj)
{
System.out.println(“Error”);
}
}
}
public class B
{
public static void
main (String ar[ ])
{
for
(int a = 0;a<10; a++)
{
A
obj = new A();
obj.start();
}
}
}
Output will be:
I am thread
.
.
.
I am thread
In the above program, we have two classes A and B. In class A, we
are extending Thread class to get the run method so that we can have a thread
code in that method. In that method, we have just one line of code i.e. “I am
Thread”. We have to write this code in try catch block to handle any exception
occurred.
In class B, we have main method in which we have a loop to get the
result of thread multiple times by creating the object of class A and calling
the start method using the object.
Example 2
class A extends Thread
{
public void run()
try
{
{
System.out.println (“Currently running Thread is “ +
Thread.currentThread().getId());
}
Catch (Exception
obj)
{
System.out.println
(“Error”);
}
}
}
public class B
{
public static void
main (String ar[ ])
{
for
(int a = 0;a<10;a++)
{
A
obj = new A();
obj.start();
}
}
}
Output will be:
Currently running thread is THREADNUMBER
Currently running thread is THREADNUMBER
Currently running thread is THREADNUMBER
Currently running thread is THREADNUMBER
.
.
.
Using Runnable Interface to create Thread
We can create Threads in Java using interface method. Here in this
method, we have to implement an interface in which we have run method. And we
have to create object of Thread class to call the start method.
Example Program
class A implements Runnable
{
public void run()
{
Try
{
System.out.println
(“Thread is running”);
}
Catch(Exception
obj)
{
System.out.println
(“Error”);
}
}
}
class B
{
public static void
main(String ar[ ])
{
for
(int a = 0; a<10;a++)
{
Thread
obj = new Thread(new A());
obj.start();
}
}
}
Output will be:
Thread is running
.
.
.
Thread is running
Example Program 2
class A implements Runnable
{
public void run()
{
Try
{
System.out.println (“Thread “ + Thread.currentThread().getId()+ “
is running”);
}
catch(Exception
obj)
{
System.out.println
(“Error”);
}
}
}
class B
{
public static void
main(String ar[ ])
{
for
(int a = 0; a<10;a++)
{
Thread
obj = new Thread(new A());
obj.start();
}
}
}
Output will be:
Thread THREADNUMBER is running
Thread THREADNUMBER is running
Thread THREADNUMBER is running
.
.
.
Thread THREADNUMBER is running