[Java] New Thread generation method (2)

Following # ①

Since ① is a method of inheritance, there are various inconveniences in use. Instead, method (2) will be used more often when creating threads.

Method (2): Create a thread using a class that implements the Runnable interface

As a step:

-Create a subclass that implements the runnable interface -Override of run () method -Instantiate the created runnable class -Create a new Thread instance with the Thread constructor using the generated runnable instance as an argument. -Call the start () method on the Thread instance

I actually wrote it: public class ThreadTest2 implements Runnable{

@Override
public void run(){
	for (int i = 0; i < 5; i++){

System.out.println ("Print from new thread"); } } }

public class Sample2 {

public void main (String[] args){
	ThreadTest2 th1 = new ThreadTest2();
	Thread thread = new Thread(th1);
	thread.start();
	
}

}

The point here is Thread thread = new Thread(th1);

The above code is a method to create a new thread by using the constructor of Thread class. A new Thread class can be created by using a class that implements the runnable interface as an argument.

Thread(Runnable target) Assign a new Thread object.

If the Thread class can be newly created, the rest is the same as ①, just start the thread with the start () method and let the scheduler execute it.

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
[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 12 new feature summary
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
About method splitting (Java)
Studying Java 8 (see method)
Java programming (class method)
Thread safe summary ~ Java ~
[Java] Password generation (Pasay)
What's new in Java 8
Java 10 new feature summary
[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
Create a java method [Memo] [java11]
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] Object-oriented syntax --class method / argument
Automatic photo resizing method in Java
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