83 lines
1.9 KiB
Java
83 lines
1.9 KiB
Java
/*
|
|
* Copyright (c) 2024. Elex. All Rights Reesrved.
|
|
* https://www.elex-project.com/
|
|
*/
|
|
|
|
package com.elex_project.examples.thread;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.List;
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
public class ThreadExample {
|
|
|
|
public void something(){
|
|
final SwingWorker<Image[], Integer> swingWorker = new SwingWorker<Image[], Integer>() {
|
|
@Override
|
|
protected Image[] doInBackground() throws Exception {
|
|
Image[] images = new Image[]{};
|
|
// do something
|
|
int i=0;
|
|
while (i<100){
|
|
publish(i);
|
|
i++;
|
|
}
|
|
return images;
|
|
}
|
|
|
|
@Override
|
|
protected void process(List<Integer> chunks) {
|
|
|
|
}
|
|
|
|
@Override
|
|
protected void done() {
|
|
|
|
}
|
|
};
|
|
swingWorker.execute();
|
|
|
|
try {
|
|
Image[] imgs = swingWorker.get(); // 작업이 끝날 때까지 대기 상태가 된다.
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public void something2(){
|
|
final Timer timer = new Timer(0, new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent actionEvent) {
|
|
|
|
}
|
|
});
|
|
timer.start();
|
|
}
|
|
public static void main(String... args) {
|
|
// 컨텐트 패널에 버튼 등의 컴포넌트를 구성한다.
|
|
final JPanel contentPane = new JPanel();
|
|
contentPane.setBackground(Color.PINK);
|
|
|
|
// 프레임을 생성한다.
|
|
final JFrame jFrame = new JFrame();
|
|
jFrame.setTitle("Example");
|
|
jFrame.setSize(800, 600);
|
|
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
jFrame.setContentPane(contentPane);
|
|
|
|
// 이벤트 디스패치 스레드에서 프레임을 표시한다.
|
|
SwingUtilities.invokeLater(() -> {
|
|
jFrame.setVisible(true);
|
|
});
|
|
|
|
// 어플리케이션 종료시에 처리할 내용은 셧다운 훅을 사용한다.
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
// do something
|
|
}));
|
|
|
|
}
|
|
}
|