Slide 6
Slide 6 text
Copyright © 2023, Oracle and/or its affiliates
6
java.lang.Thread のコンストラクタに java.lang.Runnable インターフェースの実装を渡す
もちろん、ラムダ式でもOK
Java でスレッドを扱う方法アレコレ 2/3
java.lang.Thread, java.lang.Runnable
public class SimpleRunnable implements Runnable {
@Override
public void run() {
System.out.println(String.format("[%s]: %s", Thread.currentThread().getName(), "Hello world!"));
}
}
public static void main(String[] args) {
System.out.println(String.format("[%s]: %s", Thread.currentThread().getName(), "Hello world!"));
Thread thread = new Thread(() -> {
System.out.println(String.format("[%s]: %s", Thread.currentThread().getName(), "Hello world!"));
});
thread.start();
}
Thread thread = new Thread(new SimpleRunnable());
thread.start();