123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Java Multithreading Basics | Creating And Running Threads In Java With Examples

By Author: Infocampus
Total Articles: 396

Conceptual basics of multithreading

Usually when we talk of multi-programming we refer to multiple programs running on a processor. A program is a set of instructions which when executed provides a specific functionality. An example of a commonly used program is a word processor such as MS Word. From the operating system point-of-view executing instances of programs are individual processes with each process having its own memory address space allocated to it.
As a process runs, it might need to take care of multiple internal tasks in parallel in order to deliver the complete set of functionalities which it offers. For example, as a person types in the word processor, there are multiple tasks running in the background. One task is responsible for saving changes in the document in a recovery file. Another task checks for spelling and grammatical errors as the user types. Yet another task does the basic task of displaying what a user types in the editor and so on.
All the above mentioned tasks are internal to the word processor program, i.e. they share the same memory address space which has been allocated to the word processor by the operating system. At the same time these internal tasks need to execute together or in parallel to ensure the program’s functionalities are available to its users at the same time. Each of these parallel tasks are the smallest units of independent work carried out by program, and are instances of individual threads working to make the whole program work together.
To summarize the relation between programs, processes and threads. A program in execution is a process, and an executing process can have multiple threads running in parallel within it to accomplish multiple tasks that the process needs to carry out. The diagram below depicts the relationship between processes and threads.
A thread in Java is the smallest unit of dispatchable code which can be executed. As the bigger program executes in a process space of its own, its multiple internal threads carry out their individual tasks and communicate with each other to make the program work as a whole. These internal threads of work use a portion of the memory address space allocated to their parent program/process. As we will see in detail in forthcoming tutorials on concurrency management, this sharing of memory space has both advantages(inter-thread communication) and disadvantages(synchronization issues).
Thread management classes in Java are all designed around management of parallel internal threads of work over their entire lifecycle – from creation to destruction. As we study basic and advanced features for multithreading in Java in this and upcoming tutorials in Java Concurrency series, we will cover all aspects related to defining, managing and optimizing threads.
To run a piece of functionality in parallel, you will need to first encapsulate it as a separate thread and then execute that thread. Let us now learn how to define and run individual thread instances as per our needs.
Creating and running a thread defined by extending Thread class

The first way of defining your own thread functionality involves sub-classing the java.lang.Thread class. An instance of Thread class holds a single thread of execution of a running program.
Creation of a new parallel thread using Thread class involves the following 3 steps –
1. Define a class which extends Thread class.
2. Override the run() method of Thread class, and write your custom thread logic in it.
3. Invoke the start() method on your custom Thread subclass to start the thread execution.
Creating and running a thread defined by implementing Runnable interface

The second way to create a parallel thread is by implementing java.lang.Runnable interface. There are 4 steps involved in creating and running a separate thread using Runnable –
1. Define a class which implements Runnable interface.
2. Override the run() method of Runnable interface, and write your custom thread logic in it.
3. Wrap the Runnable instance inside a new Thread instance. This is accomplished by using one of the multiple constructors provided by Thread class which accept the Runnable interface as parameter.
4. Invoke the start() method on the newly created Thread instance to start the thread execution.

More About the Author

We offer innovative and customized Java Training in Bangalore. Best Java course in Bangalore with Certified Experts. Go to free demo classes, get prepared by 8yrs experienced tutors and furthermore get hands on live project. Visit: http://infocampus.co.in/java-training-bangalore.html

Total Views: 65Word Count: 672See All articles From Author

Computer Programming Articles

1. Test Automation – Importance Of Selenium Ide With Features
Author: Siyaram ray

2. The Effect Of Chicken Cage On Temperature Regulation Of Chicken House
Author: The effect of chicken cage on temperature regulati

3. The Chicken House Needs Ventilation
Author: The chicken house needs ventilation

4. Automation Testing - Instructions For Integrating Jenkins With Selenium Webdriver
Author: Siyaram ray

5. 10 Performance Enhancement Tips For Mobile App Development
Author: august sin

6. E-commerce Software Provider Company In India For Best E-commerce Solutions
Author: Zentryx Tech

7. Automation Testing - Selenium Webdriver Testing Tool
Author: Siyaram ray

8. Career Opportunities As A Selenium Tester
Author: Siyaram ray

9. How To Do A Good Job In Heatstroke Prevention In Chicken Farms
Author: How to do a good job in heatstroke prevention in c

10. Selenium Testing Tool Is More Used In Automation Testing
Author: Siyaram ray

11. Simplify For Better Sales: Tips For Optimizing The Checkout Page
Author: Shailesh Gajjar

12. Did Web Development Training Is Require
Author: Siyaram ray

13. Join Selenium Training In Bangalore To Become Selenium Master
Author: Siyaram ray

14. Direct Mail Marketing Services – Why It Is An Integral Part Of Your Advertising Campaign
Author: Daniel Mena

15. Building Advance Website On Drupal Cms – A Scalable Option For Your Business
Author: Zentryx Tech

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: