Thread

이규은·2021년 10월 15일
0

스레드

목록 보기
1/4

프로세스

프로세스란 단순히 실행중인 프로그램이라고 할 수 있다.
사용자가 작성한 프로그램이 운영체제에 의해 메모리 공간을 할당받아 실행 중인 것을 말한다.
프로세스는 프로그램에 사용되는 데이터와 메모리 등의 자원과 스레드로 구성되어 있다

스레드

스레드란 프로세스 내에서 작업을 수행하는 주체를 의미한다.
모든 프로세스는 한 개 이상의 스레드가 존재한다.
두 개 이상의 스레드를 가진 프로세스를 멀티스레드 프로세스 라고 한다.

Thread

public class ThreadStudy extends Thread {
    public void run() {
        System.out.println("thread run.");
    }

    public static void main(String[] args) {
        ThreadStudy threadStudy = new ThreadStudy();
        threadStudy.start();
    }
}


Thread 클래스를 상속 받고 run 메소드를 구현한다.
threadStudy.start() 를 실행하면 run 메소드가 수행이 된다.

Thread 클래스는 start 실행시 run 메소드가 수행되도록 되어있다.


public class ThreadStudy extends Thread {
    int seq;

    public ThreadStudy(int seq) {
        this.seq = seq;
    }

    public void run() {
        System.out.println(this.seq + " thread start.");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        System.out.println(this.seq + " thread end.");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread t = new ThreadStudy(i);
            t.start();
        }
        System.out.println("main end.");
    }
}

스레드는 순서에 상관없이 동시에 실해오딘다.
쓰레드가 종료되기 전에 main 메소드가 종료된다.

Join

import java.util.ArrayList;

public class ThreadStudy extends Thread {
    int seq;
    public ThreadStudy(int seq) {
        this.seq = seq;
    }
    public void run() {
        System.out.println(this.seq + "thread start");

        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        System.out.println(this.seq + "thread end");
    }
    public static void main(String[] args) {
        ArrayList<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Thread t = new ThreadStudy(i);
            t.start();
            threads.add(t);
        }

        for (Thread t : threads) {
            try {
                t.join();
            } catch (Exception e) {

            }
        }
        System.out.println("main end");
    }
}

join 메소드는 스레드가 종료될때까지 기다리게 하는 메서드이다.

Runnable

import java.util.ArrayList;

public class RunnableStudy implements Runnable {
    int seq;

    public RunnableStudy(int seq) {
        this.seq = seq;
    }

    public void run() {
        System.out.println(this.seq + " thread start");

        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        System.out.println(this.seq + "thread end");
    }

    public static void main(String[] args) {
        ArrayList<Thread> threads = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new RunnableStudy(i));
            t.start();
            threads.add(t);
        }

        for (Thread t : threads) {
            try {
                t.join();
            } catch (Exception e) {

            }
        }
        System.out.println("main end");
    }
}

Runnable은 run 메소드를 구현하도록 강제한다.

profile
안녕하세요

0개의 댓글