자바

[Java] Thread

devjones 2020. 10. 22. 21:10

1.쓰레드의 도입

 

2. Creating a Thread

가. Thread 클래스를 상속하고 run() 메소드를 오버라이딩하거나

public class MyClass extends Thread {
  public void run() {
    System.out.println("This code is running in a thread");
  }
}

나. Runnable 인터페이스를 구현한다.

public class MyClass implements Runnable {
  public void run() {
    System.out.println("This code is running in a thread");
  }
}

 

3. Running Threads

가.의 경우 인스턴스 생성 후 start() 메소드 호출

the thread can be run by creating an instance of the class and call its start() method

public class MyClass extends Thread {
  public static void main(String[] args) {
    MyClass thread = new MyClass();
    thread.start();
    System.out.println("This code is outside of the thread");
  }
  public void run() {
    System.out.println("This code is running in a thread");
  }
}

 

나.의 경우

the thread can be run by passing an instance of the class to a Thread object's constructor and then calling the thread's start() method

public class MyClass implements Runnable {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    Thread thread = new Thread(obj);
    thread.start();
    System.out.println("This code is outside of the thread");
  }
  public void run() {
    System.out.println("This code is running in a thread");
  }
}

 

4. 상속이랑 구현차이

Differences between "extending" and "implementing" Threads

The major difference is that when a class extends the Thread class, you cannot extend any other class, but by implementing the Runnable interface, it is possible to extend from another class as well, like: class MyClass extends OtherClass implements Runnable.

 

5. Concurrency Problem

Because threads run at the same time as other parts of the program, there is no way to know in which order the code will run. When the threads and main program are reading and writing the same variables, the values are unpredictable. The problems that result from this are called concurrency problems.

 

예)

A code example where the value of the variable amount is unpredictable

public class MyClass extends Thread {
  public static int amount = 0;

  public static void main(String[] args) {
    MyClass thread = new MyClass();
    thread.start();
    System.out.println(amount);
    amount++;
    System.out.println(amount);
  }

  public void run() {
    amount++;
  }
}

 

해결

To avoid concurrency problems, it is best to share as few attributes between threads as possible. If attributes need to be shared, one possible solution is to use the isAlive() method of the thread to check whether the thread has finished running before using any attributes that the thread can change.

 

Use isAlive() to prevent concurrency problems:

public class MyClass extends Thread {
  public static int amount = 0;

  public static void main(String[] args) {
    MyClass thread = new MyClass();
    thread.start();
    // Wait for the thread to finish
    while(thread.isAlive()) {
    System.out.println("Waiting...");
  }
  // Update amount and print its value
  System.out.println("Main: " + amount);
  amount++;
  System.out.println("Main: " + amount);
  }
  public void run() {
    amount++;
  }
}