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:
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.
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.
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
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!...
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:
- Creating an object of the Thread class
- 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
- By inheriting your class from the Thread class
- By implementing the Runnable interface in your class
- 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!...






Nice and Valuable information you explained in this article I loved it more, it useful for me a lot. Bookmarked your site.
ReplyDeleteRegards,
Java Training in Hyderabad
Java Training in India
Java Training Institutes in India
Java Training Institutes in Hyderabad
Java Course in Hyderabad
Java Training
simply awesome explanation ...here i got one more useful example for method reference and got a new way to create thread in java
ReplyDelete
ReplyDeleteThanks for sharing this good blog.It's amazing blog Core Java Online Training Hyderabad
Nice Blog. Thanks for your's info
ReplyDeleteVisit: Best Java Course
Nice blog for students as well as those persons who want guidance..
ReplyDeleteThanks for sharing such a useful contant with us
Digitla Marketing Training in Jaipur
Php Training in Jaipur
Android Training in Jaipur
.Net Training in Jaipur
C C++ Training in Jaipur
Java Training in Jaipur
Software Testing Training in Jaipur
Tally Training in Jaipur
Hardware and Networking Training in Jaipur
Networking Training in Jaipur
Thanks for sharing this...
ReplyDeleteLeanpitch provides online training DevOps during this lockdown period everyone can use it wisely.
DevOps Online Training
Thanks for sharing this...
ReplyDeleteLeanpitch provides online training Scrum Master during this lockdown period everyone can use it wisely.
DevOps Online Training
Scrum Values
Hello,
ReplyDeleteGreat article with super informative points
Digital Marketing In Jaipur
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!
ReplyDeleteAll About 바둑이사이트 섯다 주소 Program Had Helped Many Peoples To Earn Money
ReplyDeleteThis 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!
ReplyDeleteJava Language, It's Future Scope and Understanding the Features
KEYTOTO merupakan platform togel online 4D resmi tahun 2026 yang dirancang untuk memberikan pengalaman bermain yang lengkap, aman, dan terpercaya bagi para penggunanya. Situs ini menghadirkan berbagai pilihan pasaran togel terlengkap dari berbagai negara, sehingga pemain memiliki banyak opsi untuk memasang angka sesuai dengan preferensi mereka. Dengan sistem yang modern dan terus diperbarui, KEYTOTO memastikan setiap proses taruhan berjalan secara cepat, akurat, dan transparan.
ReplyDeleteSelain menawarkan variasi pasaran yang luas, KEYTOTO juga dikenal dengan link akses yang stabil dan terpercaya, memudahkan pengguna untuk login kapan saja tanpa hambatan. Hal ini sangat penting untuk menjaga kenyamanan pemain, terutama saat mengakses situs di waktu-waktu sibuk. Tampilan platform yang sederhana dan ramah pengguna memungkinkan baik pemula maupun pemain berpengalaman untuk menavigasi fitur dengan mudah tanpa kebingungan.
Dari sisi keamanan, KEYTOTO mengutamakan perlindungan data pribadi dan transaksi finansial dengan sistem enkripsi yang canggih, sehingga setiap aktivitas pengguna tetap terjaga kerahasiaannya. Proses pendaftaran juga dibuat cepat dan praktis, memungkinkan siapa saja untuk segera bergabung dan mulai bermain dalam hitungan menit. Selain itu, layanan dukungan pelanggan tersedia untuk membantu menjawab pertanyaan atau menyelesaikan kendala yang mungkin dihadapi pengguna.
Dengan kombinasi fitur lengkap, akses mudah, serta komitmen terhadap keamanan dan kenyamanan, KEYTOTO menjadi pilihan ideal bagi para pecinta togel online yang mencari platform terpercaya untuk bermain. agen togel online
KEYTOTO kini hadir sebagai platform yang membawa konsep modern dalam dunia hiburan digital, dengan menawarkan beragam pilihan permainan slot online yang dapat diakses dengan mudah dalam satu situs terpadu. Platform ini dirancang untuk memenuhi kebutuhan para pengguna yang menginginkan pengalaman bermain yang praktis, cepat, dan menyenangkan tanpa harus berpindah-pindah layanan. Dengan tampilan antarmuka yang ramah pengguna, KEYTOTO memastikan bahwa baik pemain pemula maupun yang sudah berpengalaman dapat menjelajahi berbagai fitur yang tersedia tanpa kesulitan.
ReplyDeleteSelain kemudahan akses, KEYTOTO juga menonjolkan variasi permainan yang lengkap, mulai dari slot klasik hingga slot dengan tema dan fitur modern yang menarik. Setiap permainan dirancang dengan kualitas grafis yang baik serta sistem yang responsif, sehingga memberikan pengalaman bermain yang lebih imersif. Platform ini juga terus melakukan pembaruan untuk menghadirkan permainan terbaru agar pengguna tidak merasa bosan.
Keamanan dan kenyamanan pengguna menjadi prioritas utama, dengan sistem yang dirancang untuk menjaga data dan aktivitas tetap terlindungi. Proses pendaftaran hingga penggunaan layanan dibuat sederhana agar siapa saja dapat langsung menikmati hiburan yang ditawarkan. Dengan kombinasi antara teknologi modern, variasi permainan, dan kemudahan penggunaan, KEYTOTO berupaya menjadi pilihan utama bagi para pencari hiburan digital yang praktis dan berkualitas dalam satu tempat. situs slot online
KIKOTOTO merupakan salah satu link situs slot gacor hari ini yang banyak dicari oleh para pecinta permainan slot online karena menawarkan pengalaman bermain yang seru, aman, dan berpeluang besar mendapatkan kemenangan maksimal atau maxwin. Sebagai platform yang dikenal luas di kalangan pemain slot online, KIKOTOTO hadir dengan berbagai pilihan permainan terbaik dari provider ternama yang sudah terbukti populer dan terpercaya. Tidak hanya menyediakan game dengan tampilan menarik dan fitur lengkap, situs ini juga menawarkan tingkat kemenangan tinggi yang membuat para pemain semakin percaya untuk bermain setiap hari.
ReplyDeleteSelain dikenal sebagai situs slot gacor, KIKOTOTO juga menjadi pilihan utama bagi pemain yang mencari agen Slot88 resmi terpercaya dengan pelayanan profesional dan sistem keamanan terbaik. Semua transaksi deposit maupun withdraw diproses dengan cepat sehingga pemain dapat menikmati permainan tanpa hambatan. Dukungan layanan pelanggan yang aktif selama 24 jam juga menjadi salah satu keunggulan yang membuat para member merasa nyaman dan aman saat bermain.
KIKOTOTO menyediakan berbagai bonus menarik, mulai dari bonus member baru, cashback harian, hingga promo spesial yang dapat meningkatkan peluang kemenangan pemain. Dengan akses yang mudah melalui berbagai perangkat, baik desktop maupun smartphone, pemain dapat menikmati permainan kapan saja dan di mana saja. Karena itulah, KIKOTOTO terus menjadi rekomendasi utama bagi para pencinta slot online yang ingin mencari situs slot gacor terpercaya dengan peluang maxwin terbesar setiap harinya. link slot88