Thread

Jan 03, 2024
Thread
notion image
데이터 엑세스 순서
하드디스크
RAM
CPU
엑추에이터 핀 - 원판 돌면서 핀이 수평이동으로 데이터 엑세스
엑추에이터 핀 - 원판 돌면서 핀이 수평이동으로 데이터 엑세스
  • Random 엑세스 - Table 인덱스 번호로 다이렉트 엑세스 (운영체인 인도우가 실행)
  • 메모리의 데이터를 하드디스크에 영구히 저장 (=Input, Commit, Write)
  • RPM 원판 회전 시간 ⇒ 한 바퀴 도는 시간(회전율) + 핀 이동 시간(seek) = 풀스캔 시간
 
  • SWAP(프로세스 스케줄링): 당장 사용하지 않는 메모리 버리고 사용할 메모리를 끌어옴 (OS 자동)
  • 가상 메모리: 당장 사용할 메모리 공간이 없을 때 가상으로 생성
  • 프로세스: 하드디스크에서 사용할 프로그램 클릭 → RAM에 떠서 실행되는 것 이를 관리하는 것: CPU Proccessor
 
자바가 운영체제에서 임대
⇒ SystemCall, NativeCall / UX ↑
자바가 운영체제에서 임대 ⇒ SystemCall, NativeCall / UX ↑
public class ThreadEx01 { public static void start1() { for (int i = 1; i <= 5; i++) { try { System.out.println("start1 thread: " + i); Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public static void start2() { for (int i = 1; i <= 5; i++) { try { System.out.println("start2 thread: " + i); Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } // main thread public static void main(String[] args) { Thread t1 = new Thread(() -> { start1(); }); t1.start(); // 실을 만드는 것 Thread t2 = new Thread(() -> { start2(); }); t2.start(); // 실을 시작 하는 것 } // main 스레드 종료 }
메인 스레드가 종료되어도 다른 스레드 하나라도 있으면 종료 X
class MyFile{ // 하드디스크 기록(I/O) public void write(){ try{ Thread.sleep(5000); System.out.println("파일 쓰기 완료"); }catch (InterruptedException e){ throw new RuntimeException(e); } } } class painter{ public void painting(){ System.out.println("그림 그리기 완료"); } } public class TreadEx02 { public static void main(String[] args) { new Thread(() -> { MyFile mf = new MyFile(); mf.write(); }).start(); painter p = new painter(); p.painting(); } }
new Thread(( ) -> { }}; ⇒ 타겟 - 새로운 실의 길이 Thread → runable 타입
 
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ThreadEx03 extends JFrame { private boolean state = true; private boolean state1 = true; private int count = 0; private int count2 = 0; private JLabel countLabel; private JLabel count2Label; public ThreadEx03() { setTitle("숫자 카운터 프로그램"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 레이아웃 매니저 설정 setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // 숫자를 표시할 레이블 생성 countLabel = new JLabel("숫자1: " + count); count2Label = new JLabel("숫자2: " + count2); countLabel.setAlignmentX(CENTER_ALIGNMENT); count2Label.setAlignmentX(CENTER_ALIGNMENT); add(countLabel); add(count2Label); // 멈춤 버튼 생성 JButton increaseButton = new JButton("멈춤"); JButton increaseButton1 = new JButton("멈춤"); increaseButton.setAlignmentX(CENTER_ALIGNMENT); increaseButton1.setAlignmentX(CENTER_ALIGNMENT); add(increaseButton); add(increaseButton1); // 버튼에 액션 리스너 추가, 람다식 increaseButton.addActionListener(e -> { state = false; }); increaseButton1.addActionListener(e -> { state1 = false; }); new Thread(() -> { while (state) { try { Thread.sleep(1000); count++; countLabel.setText("숫자1: " + count); } catch (InterruptedException ex) { throw new RuntimeException(ex); } } }).start(); new Thread(() -> { while (state1) { try { Thread.sleep(1000); count2++; count2Label.setText("숫자2: " + count2); } catch (InterruptedException ex) { throw new RuntimeException(ex); } } }).start(); setVisible(true); } public static void main(String[] args) { new ThreadEx03(); } }
람다에서 불러오는 것: run( ) 메소드 / 죽지 않는 스레드: 데몬 스레드

동기, 비동기

  • 동기적: 데이터 관점 → 양쪽 컴퓨터의 데이터 일관성 프로그램 관점 → 일의 순서가 있다는 의미
  • 비동기적: 마무리가 안된 상태에서 다음 일을 수행 (알고리즘이 여기에 포함 / 라운드로빙)
 
 
Share article

from-web-developer