Implement Thread in Java and try using anonymous class, lambda

To execute Thread processing, create an instance of Thread class and call its start method. Next, I would like to write the processing content, but if you write a class that implements an interface called Runnable and pass it to "new Thread ();" OK

java


class MyRunnable implements Runnable {
	public void run(){
		for (int i=0; i < 500; i++){
			System.out.print('*');
		}
	}
}

public class Test {
	public static void main(String[] args) {
		MyRunnable r = new MyRunnable();

		Thread t = new Thread(r);
		t.start();

		for (int i=0; i < 500; i++){
			System.out.print('.');
		}
	}
}

It's full of local variables, so I'll omit it.

java


class MyRunnable implements Runnable {
	public void run(){
		for (int i=0; i < 500; i++){
			System.out.print('*');
		}
	}
}

public class Test {
	public static void main(String[] args) {
		new Thread(new MyRunnable()).start();

		for (int i=0; i < 500; i++){
			System.out.print('.');
		}
	}
}

Use an anonymous class to combine the two into one.

java


public class Test {
	public static void main(String[] args) {
		new Thread(new Runnable() {
			public void run(){
				for (int i=0; i < 500; i++){
					System.out.print('*');
				}
			}
		}).start();

		for (int i=0; i < 500; i++){
			System.out.print('.');
		}
	}
}

An interface that has only one abstract method is called a "functional interface" in the sense that only one output is defined for each input. Starting with version 8 of Java, functional interfaces can be replaced with a special notation called lambda expressions.

Replace with lambda expression

java


public class Test {
	public static void main(String[] args) {
		new Thread(() -> {
			for (int i=0; i < 500; i++){
				System.out.print('*');
			}
		}).start();

		for (int i=0; i < 500; i++){
			System.out.print('.');
		}
	}
}

Recommended Posts

Implement Thread in Java and try using anonymous class, lambda
Try using RocksDB in Java
Implement Java Interface in JRuby class and call it from Java
StringBuffer and StringBuilder Class in Java
[Java] Try to implement using generics
Try to implement Yubaba in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Try to implement n-ary addition in Java
Try using the Stream API in Java
[Java] for Each and sorted in Lambda
Try using JSON format API in Java
Java anonymous class
Comparison of thread implementation methods in Java and lambda expression description method
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try using the COTOHA API parsing in Java
NLP4J [004] Try text analysis using natural language processing and parsing statistical processing in Java
NLP4J [003] Try text analysis using natural language processing and part-speech statistical processing in Java
[Java] Thread and Runnable
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
[Java] Sort the list using streams and lambda expressions
Write a class in Kotlin and call it in Java
How to convert A to a and a to A using AND and OR in Java
Try global hooking in Java using the JNativeHook library
Use of Abstract Class and Interface properly in Java
Try to implement tagging function using rails and js
Try scraping using java [Notes]
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
Implement two-step verification in Java
I was trapped when I generated my own class equals and hashCode in Java using IDE
Java class definition and instantiation
Implement Basic authentication in Java
Implement math combinations in Java
2 Implement simple parsing in Java
Implement Email Sending in Java
Tips for using Salesforce SOAP and Bulk API in Java
Implement functional quicksort in Java
Try using gRPC in Ruby
Implement rm -rf in Java.
Implement XML signature in Java
[Java] Get date information 10 days later using milliseconds in the Date class
Mechanism and characteristics of Collection implementation class often used in Java
[Android] Convert Map to JSON using GSON in Kotlin and Java
Implement Table Driven Test in Java 14
Try implementing GraphQL server in Java
3 Implement a simple interpreter in Java
Try to implement Yubaba in Ruby
Encoding and Decoding example in Java
Try running ruby-net-nntp and tmail in 2020
Encrypt using RSA cryptography in Java
Try running Selenuim 3.141.59 in eclipse (java)
Try using Redis with Java (jar)
Implement reCAPTCHA v3 in Java / Spring
thread safe process in java language
Implement PHP implode function in Java
Try an If expression in Java
Internal class and lambda study notes
Understanding equals and hashCode in Java