mojo's Blog
스레드 정보 출력 / 타이머 스레드 강제 종료 / flag를 이용한 스레드 종료 본문
main 스레드 정보 출력 code
public class ThreadMainEx {
public static void main(String[] args) {
long id = Thread.currentThread().getId();
String name = Thread.currentThread().getName();
int priority = Thread.currentThread().getPriority();
Thread.State s = Thread.currentThread().getState();
System.out.println("현재 스레드 이름 = "+name);
System.out.println("현재 스레드 ID = "+id);
System.out.println("현재 스레드 우선순위 값 = "+priority);
System.out.println("현재 스레드 상태 = "+s);
}
}
타이머 스레드 강제 종료 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class ThreadInterruptEx extends JFrame{
private Thread th;
class TimerRunnable implements Runnable{
private JLabel timerLabel;
TimerRunnable(JLabel timerLabel){
this.timerLabel=timerLabel;
}
public void run() {
int n=0;
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
ThreadInterruptEx(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel = new JLabel();
timerLabel.setFont(new Font("Gothic", Font.ITALIC, 80));
TimerRunnable runnable = new TimerRunnable(timerLabel);
th = new Thread(runnable);
c.add(timerLabel);
JButton btn = new JButton("kill Timer");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
th.interrupt();
JButton btn = (JButton)e.getSource();
btn.setEnabled(false);
}
});
c.add(btn);
setSize(300, 200);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ThreadInterruptEx();
}
}
flag를 이용하여 스레드를 강제 종료하는 code
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class ThreadFinishFlagEx extends JFrame{
private RandomThread th;
class RandomThread extends Thread{
private Container contentPane;
private boolean flag = false;
public RandomThread(Container contentPane) {
this.contentPane = contentPane;
}
public void finish() {
flag = true;
}
public void run() {
while(true) {
int x = ((int)(Math.random()*contentPane.getWidth()));
int y = ((int)(Math.random()*contentPane.getHeight()));
JLabel label = new JLabel("Java");
label.setSize(80,30);
label.setLocation(x, y);
contentPane.add(label);
contentPane.repaint();
try {
Thread.sleep(300);
if(flag==true) {
contentPane.removeAll();
label = new JLabel("finish");
label.setSize(80,30);
label.setLocation(100,100);
label.setForeground(Color.RED);
contentPane.add(label);
contentPane.repaint();
return;
}
}
catch(InterruptedException e) {
return;
}
}
}
}
ThreadFinishFlagEx(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
th.finish();
}
});
setSize(300, 200);
setVisible(true);
th = new RandomThread(c);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ThreadFinishFlagEx();
}
}
'Java' 카테고리의 다른 글
wait(), notify(), notifyAll() 을 이용하여 스레드를 동기화하기 (0) | 2021.08.09 |
---|---|
Thread Synchronization (0) | 2021.08.09 |
스레드 만들어보기 (0) | 2021.08.08 |
Multitasking (0) | 2021.08.08 |
명품 JAVA Programming 제 12장 실습문제 (3~4 제외) (0) | 2021.08.04 |
Comments