Two ways to start a thread in Java + @

Subclass of Thread

Implement the run () method by inheriting the Thread class.

public class Qiita {
    public static void main(String[] args) {
        new HelloThread("World!").start();
        new HelloThread("Qiita!").start();
    }
}

class HelloThread extends Thread {
    private String message;

    public HelloThread(String message) {
        this.message = message;
    }

    public void run() {
        System.out.print("Hello, " + message);
    }
}

Runnable interface

Pass the class that implements the Runnable interface to the Thread constructor.

public class Qiita {
    public static void main(String[] args) {
        new Thread(new Greeting("World!")).start();
        new Thread(new Greeting("Qiita!")).start();
    }
}

class Greeting implements Runnable {
    private String message;

    public Greeting(String message) {
        this.message = message;
    }

    public void run() {
        System.out.print("Hello, " + message);
    }
}

(+ @) Create a thread with ThreadFactory

Abstract thread creation with ThreadFactory.

public class Qiita {
    public static void main(String[] args) {
        ThreadFactory factory = Executors.defaultThreadFactory();
        factory.newThread(new Greeting("Qiita!")).start();
    }
}

Recommended Posts

Two ways to start a thread in Java + @
How to display a web page in Java
Code to escape a JSON string in Java
Try to create a bulletin board in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
[Android] Two ways to get a Bluetooth Adapter
Set up a Java GUI in a separate thread to keep the main
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
I tried to make a login function in Java
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Try to solve a restricted FizzBuzz problem in Java
[Java] How to start a new line with StringBuilder
Find a subset in Java
Java8 to start now ~ Optional ~
Java thread to understand loosely
Convert a Java byte array to a string in hexadecimal notation
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
How to store a string from ArrayList to String in Java (Personal)
Create a method to return the tax rate in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
Several ways to concatenate strings separated by commas in Java
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
I tried to make a client of RESAS-API in Java
A memo to start Java programming with VS Code (2020-04 version)
I made a Dockerfile to start Glassfish 5 using Oracle Java
[Rails] Two ways to write form_with
3 Implement a simple interpreter in Java
Multithreaded to fit in [Java] template
I created a PDF in Java.
How to make a Java container
How to learn JAVA in 7 days
thread safe process in java language
[Java] How to create a folder
Log output to file in Java
A simple sample callback in Java
Java, interface to start from beginner
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
Get stuck in a Java primer
Java, arrays to start with beginners
Java 8 ~ Stream API ~ to start now
How to make a Java array
How to concatenate strings in java
A clever way to unify loggers to SLF4J in Java / build.gradle entry / 2019
How to implement a job that uses Java API in JobScheduler
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to automatically operate a screen created in Java on Windows
Cast an array of Strings to a List of Integers in Java
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
How to make a Java calendar Summary
When seeking multiple in a Java array
How to test a private method in Java and partially mock that method
How to implement date calculation in Java