- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a thread using method reference in Java?n
Method reference is one of a way in a lambda expression to refer a method without executing it. In the body part of a lambda expression, we can call another method if they are compatible with a functional interface. We can also capture "this" and "super" keywords in method references.
In the below two examples, we can create a thread with the help of "this" and "super" keywords using method reference.
Example of this keyword
public class MethodRefThisTest { public void runBody() { for(int i = 1; i < 10; i++) { System.out.println("Square of " + i + " is: " + (i*i)); } } public static void main(String[] args) { MethodReferenceThread test = new MethodReferenceThread(); test.createThread(); } private void createThread() { new Thread(this::runBody).start(); // method reference } }
Output
Square of 1 is: 1 Square of 2 is: 4 Square of 3 is: 9 Square of 4 is: 16 Square of 5 is: 25 Square of 6 is: 36 Square of 7 is: 49 Square of 8 is: 64 Square of 9 is: 81
Example of super keyword
class SuperReference { public void runBody() { for(int i = 1; i < 10; i++) { System.out.println("Square of " + i +" is: " + (i*i)); } } } public class MethodRefSuperTest extends SuperReference { public static void main(String[] args) { MethodRefSuperTest test = new MethodRefSuperTest(); test.createThread(); } private void createThread() { new Thread(super::runBody).start(); // method reference } }
Output
Square of 1 is: 1 Square of 2 is: 4 Square of 3 is: 9 Square of 4 is: 16 Square of 5 is: 25 Square of 6 is: 36 Square of 7 is: 49 Square of 8 is: 64 Square of 9 is: 81
- Related Articles
- How to create a thread using method reference in Java?\n
- How to create a thread using lambda expressions in Java?\n
- How to implement IntConsumer using lambda and method reference in Java?\n
- How to implement ToIntFunction using lambda and method reference in Java?\n
- How to sort a list using Comparator with method reference in Java 8?\n
- How to create a thread in Java
- How to create a thread by using anonymous class in Java?
- What method is used to create a daemon thread in Java?
- How to find a maximum value in a collection using method reference in Java?
- How to implement LongPredicate using lambda and method reference in Java?
- How to implement LongSupplier using lambda and method reference in Java?
- How to implement DoubleUnaryOperator using lambda and method reference in Java?
- How to implement DoublePredicate using lambda and method reference in Java?
- How to implement LongBinaryOperator using lambda and method reference in Java?
- How to implement LongFunction using lambda and method reference in Java?
- How to implement DoubleSupplier using lambda and method reference in Java?

Advertisements