Saturday, July 25, 2015

How to create thread in Java 8

In this article, we'll see a new way to create thread in Java 8 and also revise the basic two ways to create thread with the help of lambda expression. Main focus of the topic is method reference and how to use thread with lambda expression. If you want to revise the basic concept of thread check this post and this post for lambda concept.


Creating a Thread in Java

The Java API makes it easy to work with threads. It lets you represent a thread as an object. An object of the  java.lang.Thread class represents a thread. Creating and using a thread in Java is as simple as creating an object of the Thread class and using that object in a program. Let’s start with the simplest example of creating a thread in Java. There are at least two steps involved in working with a thread:

  1. Creating an object of the Thread class
  2. Invoking the start() method of the Thread class to start the thread

Creating an object of the Thread class is the same as creating an object of any other classes in Java. In its simplest form, you can use the default constructor of the Thread class to create a Thread object.
// create a new thread
Thread simpleThread  = new Thread();

Creating an object of the Thread class allocates memory for that object on the heap. It does not start or run the thread. After you have created an object of the Thread class, you must call its start() method to start the thread represented by that object.
//start the thread
simpleThread.start();


Example 
The start() method returns after doing some housekeeping work. It puts the thread in the runnable state. In this state, the thread is ready to receive the CPU time. Note that invoking the start() method of a Thread object does not guarantee “when” this thread will start getting the CPU time. That is, it does not guarantee when the thread will start running. It just schedules the thread to receive the CPU time.



Three ways to create thread
There are three ways you can specify your code to be executed by a thread

  1. By inheriting your class from the Thread class
  2. By implementing the Runnable interface in your class
  3. By using the method reference to a method that takes no parameters and returns void


Inheriting Your Class from the Thread Class
When you inherit your class from the Thread class, you should override the run() method and provide the code to be executed by the thread.


Implementing the Runnable Interface
You can create a class that implements the java.lang.Runnable interface. Runnable is a functional interface and it is declared as follows

@FunctionalInterface
public interface Runnable



Using a Method Reference
Check this post for detail on method reference. From Java 8, you can use the method reference of a method of any class that takes no parameters and returns void as the code to be executed by a thread.

The following code declares a ThreadTest class that contains an execute() method. The method contains the code to be executed in a thread.


The thread will execute the code contained in the execute() method of the ThreadTest class

Final example




If you know anyone who has started learning Java, why not help them out! Just share this post with them.Thanks for studying today!...

11 comments:

  1. simply awesome explanation ...here i got one more useful example for method reference and got a new way to create thread in java

    ReplyDelete
  2. Thanks for sharing this...
    Leanpitch provides online training DevOps during this lockdown period everyone can use it wisely.
    DevOps Online Training

    ReplyDelete
  3. Thanks for sharing this...
    Leanpitch provides online training Scrum Master during this lockdown period everyone can use it wisely.
    DevOps Online Training
    Scrum Values

    ReplyDelete
  4. You write an interesting blog, which I enjoy reading. This blogger guide is very helpful. I have also written a well-researched article in addition to my most recent one. Click here to read it. Click here for more information about the How to Click Faster when Playing Games. Spread the word!

    ReplyDelete
  5. All About 바둑이사이트 섯다 주소 Program Had Helped Many Peoples To Earn Money

    ReplyDelete
  6. This blog is a valuable resource for Java developers who want to learn about creating threads in Java 8 and make use of lambda expressions and method references for cleaner and more concise code. The author's expertise in the subject matter is evident, and the blog offers practical examples that readers can implement and experiment with on their own. Great job!
    Java Language, It's Future Scope and Understanding the Features

    ReplyDelete