일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Docker Desktop
- Swing
- GIT
- AssertJ
- JavaScript
- class
- 메소드
- 자바
- synchronized
- JS
- c#
- 클래스
- 파이썬
- 프로그래머스스쿨
- array
- join()
- SpringBoot
- 자바스크립트
- 스프링부트
- event
- 저장소
- Dict
- SSL
- 객체
- docker
- Python
- 배열
- StringBuilder
- thread
- Java
- Today
- Total
목록thread (8)
정리노트

이어달리기 package q7; import javax.swing.*; import java.io.*; public class Q7 extends JFrame{ class MyThread extends Thread { JLabel label; int x, y, end; MyThread(String name, int xn, int yn) { x = xn; y = yn; end = x+500; label = new JLabel(); label.setIcon(new ImageIcon(name)); label.setBounds(x, y, 100, 100); add(label); } public void run() { while (x+100 < end) { x += 10; label.setBounds(x, y, ..

0부터 99가 담긴 List의 값을 두 개의 스레드로 0~49 / 50~99 의 합을 구한다. 동작은 겹치지 않도록 join()을 사용하도록 한다. package q5; import java.util.ArrayList; public class Q5 extends Thread { ArrayList list; static int index = 0; static int sum = 0; Q5(ArrayList lst) { list = lst; } public void run() { for (int i = 0; i < 50; i++) { sum += list.get(index); index++; } System.out.println(getName()+ " 종료, 현재 index: " + index); System..

package q4; import java.awt.*; import javax.swing.*; import java.io.*; public class Q4 extends JFrame { int x, y, width, height; Q4() { setSize(500, 300); x = 50; y = 50; width = 400; height = 200; MyPanel p = new MyPanel(); add(p); Thread t = new Thread(p); t.start(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public class MyPanel extends JPanel implements Runnable{ public voi..

package q3; import java.awt.*; import javax.swing.*; public class Q3 extends JFrame { class MyThread extends Thread { private JLabel label; int x, y; MyThread(String file, int x, int y) { this.x = x; this.y = y; label = new JLabel(); label.setIcon(new ImageIcon(file)); label.setBounds(x, y, 100, 100); add(label); } public void run() { while (x
같은 자원에 동시에 접근하여 섞이는 것을 방지하기 위하여 동기화를 한다. package synchroPrint; /*class Printer {// 동기화 블록 사용 void print(int arr[]) { synchronized(this) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } }*/ class Printer {// 동기화 메소드 사용 synchronized void print(int arr[]) { for (int i = 0; i < arr.length..

thread의 개수보다 작업의 개수가 더 많은 경우에는 작업은 FIFO Queue에서 대기한다. Runnabel 객체를 구현하고 ThreadPoolExecutor로 보내기만 하면 됨. (ThreadPoolExecutor 는 thread로 실행하는 담당) thread가 새로 생성되는 시간을 절약하며 ThreadPoolExecutor는 thread가 계속 생성되는 것을 방지하기 위하여 몇 개의 thread를 미리 생성한 후에 pooling하여 사용한다. package threadPoolTest; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class ThreadPoolTest imple..

하나의 스레드가 다른 스레드의 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 t.interrupt()); // 버튼이 눌리면 interrupt() 실행 // 람다 getContentPane().add(button); setVisible(true); setDe..