[Java] New Thread generation method (1)

It was a little difficult to create a new thread in Java, so I tried to summarize it myself.

Method (1): Inherit the Thread class to create a thread

The steps are as follows: -Create a subclass that inherits the Thread class -Override of run () method -Instantiate the created subclass -Call the start () method on the created instance

The inherited Thread class is a class that implements the Runnable interface. The documentation defines: public class Thread extends Object implements Runnable

*link: https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Thread.html

I actually wrote it:

//Class to inherit
class ThreadTest1 extends Thread {

	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("Output from a new thread");
		}
	}
}

//Caller
public class Sample1 {

	public static void main (String[] args){
//Instance generation

		ThreadTest1 th = new ThreadTest1();
//start()Execute the method, spawn a thread and make it executable

		th.start();

		System.out.println("main thread ends");
	}
}

Execution result:

【
main thread ends
Output from a new thread
Output from a new thread
Output from a new thread
Output from a new thread
Output from a new thread
】

☆ Point here: The run () method is not called directly, but if start () puts the newly created thread into an executable state and the scheduler puts it into an executable state, the run () method is executed. In other words, I thought I kicked a new thread with start (), but in fact it just started. The actual execution is left to the scheduler.

Recommended Posts

[Java] New Thread generation method (2)
[Java] New Thread generation method (1)
[Java] Random number generation method (Random)
[Java Silver] Array generation method
java (method)
Java method
[Java] method
[Java] method
Java8 method reference
Java thread processing
[Java] forEach method
Java permutation generation
java1.8 new features
Introduction of New Generation Java Programming Guide (Java 10)
java8 method reference
Introduction of New Generation Java Programming Guide (Java 11)
[Java] Random method
[Java] split method
Introduction of New Generation Java Programming Guide (Java 12)
JAVA DB connection method
[Java] Thread and Runnable
Java learning 2 (learning calculation method)
Java 13 new feature summary
Java learning memo (method)
[Java] Stream API-Stream generation
About Java method binding
[Java ~ Method ~] Study memo (5)
Ruby Thread # [] = method notes
Studying Java 8 (see method)
Java programming (class method)
Thread safe summary ~ Java ~
[Java] Password generation (Pasay)
What's new in Java 8
[Java] Basic method notes
Java 14 new feature summary
What's new in Java 9,10,11
Java Programming Thread Runnable
Java GC method determination conditions
New features from Java7 to Java8
Java Silver Study Method Memo
Java test code method collection
[Java Silver] About equals method
Generation gap with new engineers
[Java] Timer processing implementation method
Java thread safe for you
Java methods and method overloads
Java thread to understand loosely
Benefits of Java static method
Recent Java API specification generation
New grammar for Java 12 Switch statements
Java method list (memorial) (under construction)
[Java] How to use join method
Screen transition by Post method [Java]
[Java] Object-oriented syntax-class / field / method / scope
Java comparison using the compareTo () method
Java 9 new features and sample code
[Java beginner] == operator and equals method
thread safe process in java language
What is thread safe (with Java)
Try to extract java public method
Try using IBM Java method tracing