0% found this document useful (0 votes)
18 views24 pages

WWW Journaldev Com 1079 Multithreading in Java

The document provides an overview of multithreading in Java, explaining the concept of threads, their types (user and daemon), and how to create them using the Runnable interface or by extending the Thread class. It also discusses thread priorities, the role of the thread scheduler, and various topics related to thread management such as sleep, join, states, synchronization, and thread safety. Additionally, it lists several articles for further reading on specific multithreading topics in Java.

Uploaded by

sererwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views24 pages

WWW Journaldev Com 1079 Multithreading in Java

The document provides an overview of multithreading in Java, explaining the concept of threads, their types (user and daemon), and how to create them using the Runnable interface or by extending the Thread class. It also discusses thread priorities, the role of the thread scheduler, and various topics related to thread management such as sleep, join, states, synchronization, and thread safety. Additionally, it lists several articles for further reading on specific multithreading topics in Java.

Uploaded by

sererwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

JAVA TUTORIAL #INDEX POSTS #INTERVIEW QUESTIONS RESOURCES STORE

HOME » JAVA » MULTITHREADING IN JAVA

Multithreading in Java
OCTOBER 2, 2016 BY PANKAJ — 16 COMMENTS

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Multithreading in Java is a very important topic. I have written a lot about Threads in Java. Java
Thread is a lightweight process that executes some task. Java provides multithreading support
with the Thread class and an application can create multiple threads executing concurrently.

Multithreading in Java

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
There are two types of threads in an application – user thread and daemon thread. When we
start an application, main is the first user thread created and we can create multiple user
threads as well as daemon threads. When all the user threads are executed, JVM terminates
the program.

We can set different priorities to different Threads but it doesn’t guarantee that higher priority
thread will execute first than lower priority thread. Thread scheduler is the part of Operating
System implementation and when a Thread is started, it’s execution is controlled by Thread
Scheduler and JVM doesn’t have any control on it’s execution.

We can create Threads by either implementing Runnable interface or by extending Thread


open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Class.

Thread t = new Thread(new Runnable(){


@Override
public void run() {
}
});

Above is a one liner statement to create new Thread, Here we are creating Runnable as
Anonymous Class, read this post to learn about inner class, nested class and anonymous inner
class.

In last few weeks, I have posted some useful articles on multithreading in java, you can follow
them in order to learn about Threads in Java.

Threads in Java

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
With Java 8 lambda expressions, we can create Thread in java like below too because
Runnable is a functional interface.

1. Ecommerce Web Design

2. Build A Web Site

3. Web Design Services

4. Create A Website

5. How To Build A Website

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
1. Ecommerce Web Design

2. Build A Web Site

3. Web Design Services

4. Create A Website

5. How To Build A Website

Thread t = new Thread(() -> {System.out.println("My Runnable");});


t.start();

1. Java Thread Example


This is the first post in the multithreading in java tutorial series. Read this to learn about
Process and Thread. What is the difference between Thread and Process. Benefits of
using Threads and how we can create Threads using Runnable interface and Thread
class. This post also compares Runnable interface with Thread class.

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
2. Java Thread Sleep
Java Thread sleep is used to pause the execution of current thread. We will use Thread
sleep extensively in future posts, so it’s good to know how it works and is it accurate or
not?

3. Java Thread Join


Sometimes we need to wait for other threads to finish it’s execution before we can
proceed. We can achieve this using Thread join, learn how it works and when we should
use it.

4. Java Thread States


Understanding different states of thread is important. Learn how thread changes it’s state
and how thread scheduler changes thread state.

5. Java Thread wait, notify and notifyAll


Java Object class contains three methods using which threads can communicate about
the lock status of a resource. Learn with example usage of these Object class methods in
a simple Wait-Notify implementation.

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
6. Java Thread Safety and Java Synchronization
We know that Threads share Object resources, it can lead to data corruption because
these operations are not atomic. Learn how we can achieve thread safety in java using
different methods. Read this post to learn about the correct usage of synchronization,
synchronized methods and synchronized blocks.
There are various examples of synchronized usage and the post explains what are the
issues with them.

7. Java Exception in thread main


JVM creates first thread using main method. This post explains about some common
exceptions we see in daily life and what is the root cause of them and how to fix them.

8. Thread Safety in Singleton Class


In this article, you will learn basic concepts of creating Singleton class. What are thread
safety issues with different implementation. How we can achieve thread safety in
Singleton class.

9. Java Daemon Thread

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
A simple article explaining about daemon threads and how we can create daemon
threads in java.

10. Java Thread Local


We know that threads share Object’s variables but what if we want to have thread-local
variables created at class level. Java provides ThreadLocal utility class to create thread-
local variables. Read more to learn about how we can create ThreadLocal variables in
java program.

11. Java Thread Dump


Java Thread dump provides the current threads information for the program. Java
Thread dump provides useful information to analyze performance issues with the
application. You can use thread dump to find and fix deadlock situations. This post
explains different methods that can be used to generate thread dump in java.

12. How to Analyze Deadlock and avoid it in Java


Deadlock is a situation where multiple threads are waiting for each other to release
resources causing cyclic dependency. This article discusses about the situation in which
we can get deadlock in a java program. How we can use Thread dump to find the

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
deadlock and best practices to avoid deadlock in java program.

13. Java Timer Thread


This post explains how we can use Java Timer and TimerTask classes to create jobs to
run at scheduled interval, an example program showing it’s usage and how we can
cancel the timer.

14. Java Producer Consumer Problem


Before Java 5, producer-consumer problem can be solved using wait() and notify()
methods but introduction of BlockingQueue has made it very easy. Learn how we can
use BlockingQueue to solve producer consumer problem in java.

15. Java Thread Pool


Java Thread Pool is a collection of worker threads waiting to process jobs. Java 5
introduction of Executor framework has made it very easy to create thread pool in java
using Executors and ThreadPoolExecutor classes. Learn how to use them to create
thread pool in java.

16. Java Callable Future


open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Sometimes we wish Thread could return values that we can use. Java 5 Callable can be
used in that case that is similar as Runnable interface. We can use Executor framework to
execute Callable tasks.

17. Java FutureTask Example


FutureTask class is the base concrete class that implements Future interface. We use it
with Callable implementation and Executors for asynchronous processing. FutureTask
provide implementation methods to check the state of the task and return the value to
the calling program once it’s processing is finished. It comes handy when you want to
override some of the implementation methods of the Future interface.

This list will keep growing as I write more on multithreading in java, so make sure to bookmark
it for future use.

FILED UNDER: JAVA

About Pankaj
If you have come this far, it means that you liked what you are reading. Why not reach little more
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
and connect with me directly on Google Plus, Facebook or Twitter. I would love to hear your
thoughts and opinions on my articles directly.
Recently I started creating video tutorials too, so do check out my videos on Youtube.

« Java ThreadLocal Example Java Callable Future Example »

Comments

Raed says
AUGUST 29, 2016 AT 4:52 AM

Hi i am writing about the Thread above

Thread t = new Thread(() -> {System.out.println(“My Runnable”);});


t.run();

do you mean t.run(); or t.start(), ?

thanks

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Reply

Pankaj says
AUGUST 29, 2016 AT 6:58 AM

You are right, it should be t.start()

I have updated the post.

Reply

Raed says
SEPTEMBER 8, 2016 AT 5:53 AM

Hello Pankaj

do you know that the journalDev is great place to get important inforamtion.

Reply

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Ankit Neema says
MARCH 1, 2016 AT 9:57 AM

I like your articles very much. I was going through Multithreading article and I think almost
all things are covered except about ForkJoinPool class in java. It will be of very much help
to me as well as others as all articles will get consolidated at one place only. It will be of
much help if you can write about it. Thanks

Reply

Pankaj says
JUNE 19, 2016 AT 8:36 AM

Okay, I will write on it in sometime.

Reply

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Suresh Yadam says
AUGUST 4, 2015 AT 3:37 AM

Hi Sir,

There was a spelling mistake at ( of ) in the below line, please change it sir.

We can create Threads by either implementing Runnable interface of by extending Thread


Class.

Reply

Pankaj says
JUNE 19, 2016 AT 8:37 AM

Thanks for catching the typo error, I have corrected it.

Reply
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Sikander Rafiq says
SEPTEMBER 6, 2014 AT 12:08 PM

HI,
I have tried to run Java thread example, but its run method is not calling. I just created
MyThread and HeavyRunnable class through eclipse editor and add extends and
implements Runnable by hand. Any issue. Thanks.

Reply

Pankaj says
JUNE 19, 2016 AT 8:38 AM

Without looking at your code it’s hard to tell the exact issue. Please post the code here
in comments and I will check it.

Reply
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Gitanjali says
AUGUST 13, 2014 AT 5:55 AM

it has given me a use full tips for me and it helped me to understand

Reply

Pankaj says
JUNE 19, 2016 AT 8:38 AM

you are welcome Gitanjali.

Reply

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
keval patel says
JUNE 4, 2014 AT 11:28 PM

Sir i want program definitions list in core java.

Reply

Pankaj says
JUNE 19, 2016 AT 8:39 AM

Not sure what you meant here.

Reply

Tan says
NOVEMBER 6, 2013 AT 6:07 PM

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Very helpful post. Can you post locks concept in Threads.

Reply

Pankaj says
NOVEMBER 11, 2013 AT 4:28 PM

Thanks for the input, I would post something on Thread Lock soon.

Reply

Mohamed says
SEPTEMBER 11, 2013 AT 1:54 PM

Please can you tell me how to print the Future values as in Example 16(Callable task) in
example 17(FutureTask )?

Reply
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

Name *

Email *
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Website

POST COMMENT

CONNECT WITH US

   

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
STAY UPDATED!

Name

E-Mail Address

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
I AM IN!

RECOMMENDED TUTORIALS

Java Tutorials: Java IO Tutorial, Java Regular Expressions Tutorial, Multithreading in Java, Java
Logging API Tutorial, Java Annotations,Java XML Tutorial, Collections in Java, Java Generics,
Exception Handling in Java, Java Reflection, Java Design Patterns, JDBC Tutorial
Java EE: Servlet JSP Tutorial, Struts2 Tutorial, Spring Tutorial, Hibernate Tutorial, Primefaces
Tutorial
Web Services: Apache Axis 2 Tutorial, JAX-RS Web Services Tutorial
Misc: Memcached Tutorial

Resources: Free eBooks, My Favorite Web Hosting

IMPORTANT INTERVIEW QUESTIONS

Java String Interview Questions, Java Multithreading Interview Questions, Java Programming
Interview Questions, Java Interview Questions, Java Collections Interview Questions, Java Exception
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Interview Questions

Servlet Interview Questions, JSP Interview Questions, Struts2 Interview Questions, JDBC Interview
Questions, Spring Interview Questions, Hibernate Interview Questions

© 2016 · Privacy Policy · Don't copy, it's Bad Karma · Powered by WordPress

open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com

You might also like