Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Swing
- c#
- 저장소
- 클래스
- 스프링부트
- synchronized
- 파이썬
- array
- JS
- JavaScript
- StringBuilder
- 객체
- docker
- GIT
- 프로그래머스스쿨
- event
- 메소드
- 자바스크립트
- class
- AssertJ
- 자바
- Java
- SSL
- SpringBoot
- join()
- Dict
- thread
- 배열
- Python
- Docker Desktop
Archives
- Today
- Total
정리노트
[자바/java] Thread. interrupt()로 컨트롤 본문
하나의 스레드가 다른 스레드의 interrupt()를 호출하면 해당 스레드는 중지된다.
이는 InterruptedException 로 예외처리를 하면 된다.
package threadControl;
import javax.swing.*;
import java.awt.*;
public class ThreadControl extends JFrame {
private JLabel label;
Thread t;
class Counter extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return ; // return을 해야 중단
}
label.setText(i + "");
}
}
}
public ThreadControl() {
setTitle("카운트");
setSize(400, 150);
getContentPane().setLayout(null); // 이벤트 처리가 있어 pane 사용 / panel(x)
label = new JLabel("0");
label.setBounds(0, 0, 280, 100);
label.setFont(new Font("Serif", Font.BOLD, 110));
getContentPane().add(label);
JButton button = new JButton("STOP");
button.setBounds(247, 25, 125, 23);
button.addActionListener(e -> t.interrupt()); // 버튼이 눌리면 interrupt() 실행 // 람다
getContentPane().add(button);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
t = new Counter();
t.start();
}
public static void main(String[] args) {
ThreadControl t = new ThreadControl();
}
}
728x90
'프로그래밍 > Java' 카테고리의 다른 글
[자바/java] Thread 동기화 synchronized (0) | 2023.02.04 |
---|---|
[자바/java] Thread. ThreadPool (0) | 2023.02.03 |
[자바/java] Thread . sleep(), join() 사용법기초 (0) | 2023.02.03 |
[자바/java] text파일에 저장된 단어로 행맨만들기 (0) | 2023.02.01 |
[자바/java] File BufferedReader 활용 특정 파일 실행, 문자찾기 (0) | 2023.01.31 |