LoginSignup
7
3

More than 1 year has passed since last update.

Java: Threadおさらい

Last updated at Posted at 2021-11-03

基礎知識

Thread2種類

SimpleThread.java
package playground;

public class SimpleThread extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(10000L); // 10秒停止する
                System.out.println("スレッドで動いています:" + (i+1) +"回目");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

SimpleThread2.java
package playground;

public class SimpleThread2 implements Runnable {
    public void run() {

        Thread t = Thread.currentThread(); // このメソッドを動かしているThreadを得る
        long id = t.getId();
        String name = t.getName();
        System.out.println("スレッドの識別子は" + id + "、名前は" + name + "です");
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(10000L); // 10秒停止する
                System.out.println("スレッド2で動いています:" + (i+1) +"回目");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

Thread呼び出し

ThreadUser.java
package playground;

public class ThreadUser {

    public static void main(String[] args) {
        SimpleThread t = new SimpleThread();
        t.start();

        SimpleThread2 t2 = new SimpleThread2();
        Thread t2c = new Thread(t2); 
        t2c.start();

        System.out.println("joinを始めます");

        try {
            t.join(); // スレッドでの処理が終わるまで、ここでブロックされる
            t2c.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        System.out.println("joinが終わりました");
    }
}

image.png

常駐

SimpleThreadReceiver.java
package playground;
import java.io.File;
public class SimpleThreadReceiver implements Runnable {
    public void run() {

        Thread t = Thread.currentThread(); // このメソッドを動かしているThreadを得る
        long id = t.getId();
        String name = t.getName();
        System.out.println("スレッドの識別子は" + id + "、名前は" + name + "です");
        int i = 0;
        File file = new File("C:\\dummy\\test.txt");
        boolean stop = false;

        while (!stop) {
            try {
                i++;
                Thread.sleep(10000L); // 10秒停止する
                System.out.println("スレッドReceiverで動いています:" + (i) +"回目");
                if (file.exists()) {
                    stop = true;
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

image.png

  1. JavaのThreadを常駐させる
  2. 他サービスとSocketで通信し合う
  3. それらを起動する・停止するシェルを用意する

みたいなことをして超簡単にJavaを常駐させる仕組みを作ったりしている例の説明のために書きました。

参考

以上簡単なメモ書きです。
参考になればさいわいです。

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3