First of all let me clear that reading this blog post carefully, concentration from top to bottom without missing any line, you will be able to answer some of these queries.
Concept of Multithreading
Uses of MultiThreading in Java
How to use Multithreading in Java
Concept of Multithreading
Uses of MultiThreading in Java
How to use Multithreading in Java
Multithreading
A computer Program is able to do more than one thing at a time. For an example, a Word processor can perform multiple tasks like it takes user input from keyboard while it checks for spelling. This tasks looks like they are being preformed at same time. Similarly Java program also allow multithreading or concurrency.
In Java, you can perform concurrency either by using process or threads. These both are unit of execution of a program. You can learn what there are at Threads and Processes.
To perform multithreading, you need to control activities of a thread. A thread can begin, start running, pause, resume, and finally stop. This way you can pause one thread while other thread is in execution or vice versa. One main thread is always launched by JVM(Java Virtual Machine) while you need to make other threads run, pause or stop.
To perform multithreading, you need to control activities of a thread. A thread can begin, start running, pause, resume, and finally stop. This way you can pause one thread while other thread is in execution or vice versa. One main thread is always launched by JVM(Java Virtual Machine) while you need to make other threads run, pause or stop.
Creating and Running Threads
Two ways you can create a thread is one by using thread class(extends Thread) or by implementing runnable.
Extends Thread
public class ThreadExample extends Thread
{
public void run()
{
System.out.println("New Thread");
}
public static void main(String[] args)
{
Thread one=new ThreadExample(); // notice this
one.start();
}
} //end of class
Implements Runnable
public class ThreadExample implements Runnable
{
public void run()
{
System.out.println("New Thread runnable");
}
public static void main(String[] args);
{
ThreadExample te = new ThreadExample();
Thread one=new Thread(te); // note the change.
}
} //end of class
In both of these cases, you will notice that method run() is overridden. This means that to create a thread or to assign the task in the threads, you must override the run() method. Every statement inside run method is executed while the thread is started.
Also notice the two ways the thread is created. Find the difference in the case while Thread is extended and Runnable is implemented.
Continue Multithreading at next Post.
public class ThreadExample implements Runnable
{
public void run()
{
System.out.println("New Thread runnable");
}
public static void main(String[] args);
{
ThreadExample te = new ThreadExample();
Thread one=new Thread(te); // note the change.
}
} //end of class
In both of these cases, you will notice that method run() is overridden. This means that to create a thread or to assign the task in the threads, you must override the run() method. Every statement inside run method is executed while the thread is started.
Also notice the two ways the thread is created. Find the difference in the case while Thread is extended and Runnable is implemented.
Continue Multithreading at next Post.
1 comments:
good one
Post a Comment
You can Comment here. Please make some reasonable Comments. This Blog is Dofollow so get a backlink as a Gift.
Be Sure you use Name@Keywords
Using Keywords directly may make your comments difficult to get approved.