[JAVA] Thread
- 프로그램의 명령을 실행하게끔 해주는 실행 주체.
- 개발자가 별도의 Thread를 생성하지 않는다면, 한 프로그램에 하나의 Thread 가 존재하여 해당 명령을 차례 차례 순서대로 실행시킴
class ThreadA1{
public void run() {
for(int i=1; i<=10; i++) {
System.out.printf("ThreadA1 : i -> %d\n", i);
try {
Thread.sleep(500); // 실행을 잠시(0.5초)동안 멈춤
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadA2{
public void run() {
for(int i=1; i<=10; i++) {
System.out.printf("ThreadA2 : i -> %d\n", i);
try {
Thread.sleep(500); // 실행을 잠시(0.5초)동안 멈춤
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread01 {
public static void main(String[] args) {
ThreadA1 t1 = new ThreadA1();
ThreadA2 t2 = new ThreadA2();
t1.run();
t2.run();
for(int i=1; i<=10; i++) {
System.out.printf("main : i -> %d\n", i);
try {
Thread.sleep(500); // 실행을 잠시(0.5초)동안 멈춤
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
- 프로그램의 수행 중 동시에 처리하고자 하는 작업이 생겼을때는 원래 갖고 있는 메인 Thread 외에 Thread 클래스를 추가사용하여 실행시킬 수 있음
- Thread Class: 프로그램 실행의 흐름을 분기할 수 있는 방법을 제공하는 클래스.
구현방법
1. Thread 클래스를 상속받는 방법
1-1. Thread 클래스를 상속받아 public void run() 메서드를 오버라이딩
- public void run() : 쓰레드가 생성되어 수행할 작업을 정의하는 메소드
1-2. 해당 클래스의 객체를 생성하고, start 메서드를 호출(실행)
- 만약 오버라이딩된 run 메서드를 호출하면, 쓰레드 생성실행이 아니라, 일반 메서드 호출이 되므로 반드시 start 메서드를 통해 run 으로 이어지게 호출
**start 메서드: Thread 클래스(부모 클래스) 에 있는 메서드로, 자체적으로 쓰레드 추가. 생성 후 run 메서드를 재호출하는 역할을 함.(상속된 메서드로 메서드가 표면에 보여지지는 않은 채 사용됨)
2. Runnable 인터페이스를 implements(구현)하는 방법(Thread 클래스를 상속하는 방법과 같고 효과도 거의 같음)
2-1. Runnable 인터페이스를 구현하는 클래스 작성
2-2. public void run() 오버라이딩 구현
2-3. 해당 클래스의 A 객체 생성
2-4. A 객체를 Thread 클래스의 생성자로 전달하여 Thread 객체 생성
2-5. 생성시킨 쓰레드의 start 메소드 호출
class ThreadB1 extends Thread{
public void run() {
for(int i=1; i<=10; i++) {
System.out.println("ThreadB1 : i -> " + i);
try {
Thread.sleep(500);
// sleep(500); Thread 클래스를 상속한 클래스는 sleep 메서드도 물려받았을 것이므로
// 클래스 이름을 앞에 붙이고 사용하지 않아도 됨
// sleep 메서드는 프로세서 실행에 관여하는 명령이라서 예외처리가 따라다님
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadB2 extends Thread{
public void run() {
for(int i=1; i<=10; i++) {
System.out.println("ThreadB2 : i -> " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread02 {
public static void main(String[] args) {
ThreadB1 b1 = new ThreadB1();
ThreadB2 b2 = new ThreadB2();
//b1.run();
//b2.run();
b1.start();
b2.start();
for( int i = 1 ; i <= 10 ; i++ ) {
System.out.printf("main : i -> %d\n", i);
try { Thread.sleep(500);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
class ThreadD1 implements Runnable {
public void run() {
for( int i = 1 ; i <= 10 ; i++ ) {
System.out.printf("ThreadD1 : i -> %d\n", i);
try { Thread.sleep(500);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
class ThreadD2 implements Runnable {
public void run() {
for( int i = 1 ; i <= 10 ; i++ ) {
System.out.printf("ThreadD2 : i -> %d\n", i);
try { Thread.sleep(500);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
public class Thread04 {
public static void main(String[] args) {
// Runnable 인테페이스를 구현한 쓰레드 생성
// 1. Runnable 인터페이스를 구현한 클래스의 객체 생성
ThreadD1 t1 = new ThreadD1();
// 2. 1에서 생성된 객체를 Thread 생성자에 전달인수로 전달하며 Thread 객체 생성
Thread t = new Thread(t1);
// 3. 2에서 생성된 Thread 객체를 사용하여 start 메소드 호출
t.start();
// 위의 과정을 한 라인으로 작성한 코드
new Thread(new ThreadD2()).start();
for( int i = 1 ; i <= 10 ; i++ ) {
System.out.printf("main : i -> %d\n", i);
try { Thread.sleep(500);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
import javax.swing.JOptionPane;
class ThreadC1 extends Thread{
public void run() {
for(int i=10; i > 0; i--) {
System.out.println(i);
try {
sleep(1000);
} catch(Exception e ) {
}
}
}
}
class ThreadC2 extends Thread{
public void run() {
String input = JOptionPane.showInputDialog("정답을 입력하세요.");
// 자바에서 만들어지는 윈도우 프로그램을 위한 구성요소로 윈도우 프로그래밍 가능
// 그 클래들은 javax.swing 패키지에 있고 그중 대화상자 클래스가 JOptionPane
// 위 객체의 static 메서드 실행은 화면에 단순 입력을 위한 대화창을 열고 입력을 기다림.
// 해당입력을 하고 확인을 누르면 입력한 내용은 변수 input에 저장되고 대화상자는 닫힘
System.out.println("입력하신 값은 " + input + "입니다.");
}
}
public class Thread03 {
public static void main(String[] args) {
ThreadC1 t1 = new ThreadC1();
ThreadC2 t2 = new ThreadC2();
t2.start();
t1.start();
// t1의 상태를 보고 종료시 t2도 종료되게 하면 될듯함
// 다만 종료 명령이 다소 복잡
// 기존에 사용하던 t1.stop(); 이 있지만 Thread 의 stop() 메서드는 사용중이었던
// 자원(변수, 메모리 등)의 불안정을 초래하기 때문에 지금은 사용하지 않음
}
}
new Thread().start();
- 익명 클래스를 활용한 쓰레드 생성
- 익명 클래스 : 메소드를 오버라이딩하면서 객체를 생성하는 방법
public class Thread05 {
public static void main(String[] args) {
new Thread() {
public void run() {
for( int i = 1 ; i <= 10 ; i++ ) {
System.out.printf("익명클래스 : i -> %d\n", i);
try { Thread.sleep(1000);
}catch (InterruptedException e) { e.printStackTrace(); }
}
}
}.start();
for( int i = 1 ; i <= 10 ; i++ ) {
System.out.printf("main : i -> %d\n", i);
try { Thread.sleep(1000); }
catch (InterruptedException e) {e.printStackTrace();}
}
}
}
import javax.swing.JOptionPane;
class ThreadE extends Thread{
// 스레드를 계속 실행할지를 판단할 변수 생성
private boolean state = true;
// state 변수값을 변경할 수 있는 멤버 메서드 생성
public void setState(boolean s) {
this.state = s;
}
public void run() {
// 반복실행이 계속될때마다, 스레드 실행여부가 검사됩니다 state==true
for(int i=10; i > 0 && state==true ; i--) {
System.out.println(i);
try { sleep(1000);
} catch(Exception e ) {}
}
}
}
public class Thread06 {
public static void main(String[] args) {
ThreadE t2= new ThreadE();
t2.start();
String input = JOptionPane.showInputDialog("정답을 입력하세요.");
// t2 스레드를 멈추는 위치
t2.setState(false);
System.out.println("입력하신 값은 " + input + "입니다.");
}
}