원문링크

쓰레드 정의와 시작

Defining and Starting a Thread

쓰레드의 인스턴스를 생성하는 응용프로그램은 반드시 쓰레드에 run 메서드 코드를 제공해야합니다. 이는 두가지 방법이 있습니다.

An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:

  • Runnable 객체 제공 Runnable 인터페이스는 쓰레드에서 실행하는 코드를 포함하는 단일 메서드 run을 정의합니다. 이 Runnable 객체는 쓰레드 생성자에 인자로 전달됩니다. HelloRunnable 예제 참조
    Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:

    public class HelloRunnable implements Runnable {
    
    public void run() {
        System.out.println("Hello from a thread!");
    }
    
    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }
    
    }
    
  • 서브클래스 쓰레드 쓰레드 클래스는 스스로 Runnable 인터페이스를 구현함에도 불구하고 run 메서드는 (main 메서드안에) 없습니다 어플리케이션은 HelloThread 예제처럼 자신의 run 메서드 구현체를 제공하여 Thread의 서브클래스화 할 수 있습니다.(Thread의 서브 클래스이므로 Thread로 동작 가능해짐.)
    Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example:

    public class HelloThread extends Thread {
    
    public void run() {
        System.out.println("Hello from a thread!");
    }
    
    public static void main(String args[]) {
        (new HelloThread()).start();
    }
    
    }
    

두 예제는 새 스레드를 시작하기위해 Thread.start를 호출한다.
Notice that both examples invoke Thread.start in order to start the new thread.

두 용례중 어느것을 사용하겠습니까? 첫번째 용례 Runnable 객체를 사용하는 것이 더 일반적이다 왜냐하면 Runnable 객체는 Thread가아닌 다른 클래스를 서브클래스화 할 수 있습니다. 두번째 용례는 간단한 응용프로그램에서 사용하기 쉽지만 태스크 클래스가 Thread의 하위 클래스여하는 제한이 있습니다. 이 수업에서는 실행하는 태스크인 Thread 객체로부터 Runnable 태스크를 분리하는 것에 초점을 두고있습니다. 이 방법은 더 유연하고 나중에 다를 고수준 쓰레드 관리 API에 적용할 수 있다.
Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

쓰레드 클래스는 쓰레드 관리를 위해 쓸만한 메서드들을 정의했다. 이는 쓰레드에대한 정보나 쓰레드의 상태에 영향에 대한 정적 메서들를 제공한다.(???? - the thread invoking the method.) 다른 메서드들은 다른 쓰레드로부터 호출됩니다.(쓰레드와 쓰레드 객체를 관리하는데 관련된) 우리는 이러한 메서드들을 다음 세션에서 검토할 것이다.
The Thread class defines a number of methods useful for thread management. These include static methods, which provide information about, or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and Thread object. We’ll examine some of these methods in the following sections.