본문 바로가기

Development Note

Defining and Starting a Thread

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Defining and Starting a Thread
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:

* 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:

스레드 인스턴스를 만든 어플리케이션은 아래와 같은 두 가지의 방법으로 스레드가 동작할 수 있는 코드를 제공해야 합니다.

* Runnable 객체를 제공합니다. Runnable 인터페이스는 스레드에서 수행되는 코드를 담고 있는 것을 의미하는 run 이라는 한 개의 메소드를 정의하고 있습니다. Runnable 객체는 아래 HelloRunnable 예제에서처럼 스레드 생성자에게 전해집니다.

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();

      }

}


* 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:

* 스레드 클래스의 서브 클래스가 됩니다. Thread 클래스는 Runnable 인터페이스의 run을 아무 일도 하지 않도록 하여 구현하고 있습니다. 어플리케이션은 Thread 클래스의 서브 클래스 일 수 있으며, 아래의 HelloThread 예시에서 처럼 구현한 run이라는 것을 가지고 있습니다

public class HelloThread extends Thread {

      public void run() {

            System.out.println("Hello from a thread!");

      }

      public static void main(String args[]) {

            (new HelloThread()).start();

      }

}


Notice that both examples invoke Thread.start in order to start the new thread.

   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.

새로운 스레드를 시작 시키는 대신에 Thread.start를 호출 하는 두 예제를 주목합시다.

어떤 방식을 사용하시겠습니까? Runnable 객체 서브클래스가 될 수 있기 때문에 Runnable 객체를 이용하는 첫 번째 방식이 더 보편적입니다. 두 번째 방식은 간단한 어플리케이션에서 사용하기 쉽습니다. 하지만 이것은 Thread의 서브 클래스가 되어야만 한다는 사실로 제한되어 집니다. 이 레슨에서는 첫 번째 것으로 task를 수행하는 스레드 객체로부터 Runnable task를 분할하는 첫 번째 방법에 초점을 맞추겠습니다. 이런 방법이 더 유연성 있을 뿐 아니라 후에 고 수준의 스레드 API 관리에 적합합니다.


   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.

스레드 관리의 편의를 위해 스레드 클래스는 몇 가지 메소드들을 제공합니다. 메소드를 호출하는 스레드의 정보를 제공하거나 상태에 영향을 주는 스태틱 메소드들을 제공합니다. 다른 메소드들은 스레드 객체를 관리하는 다른 스레드들로부터 호출되어 집니다. 다음 섹션에서 이런 메소드들의 예를 보도록 합시다.