My footsteps
쓰레드 본문
< 인터페이스 복습 >
package ex06.collection;
public class 장원영 implements 주연배우{
//주연배우가 해야할 연기를 따른다(임플로이먼츠)
void 일상_대화를_한다() {};
void 화내는_연기를_한다() {};
void 슬픈_연기를_한다() {};
@Override
public void 잔잔한_대화를_하다() {
일상_대화를_한다(); //자기만의 연기가 있어서 자기껄 씀
}
@Override
public void 화를_내다() {
화내는_연기를_한다();
}
@Override
public void 슬픈_연기를_하다() {
슬픈_연기를_한다();
}
//주연배우꺼 오버라이드
}
package ex06.collection;
public interface 주연배우 {
void 잔잔한_대화를_하다();
void 화를_내다();
void 슬픈_연기를_하다();
}
package ex06.collection;
public class 이도현 implements 주연배우{
public void 잔잔한_대화를_하다() {
}
public void 화를_내다() {
}
public void 슬픈_연기를_하다() {
}
}
package ex06.collection;
public class Program22 {
public static void main(String[] args) {
//대본
// 장원영 mainActor = new 장원영();
// //장원영 형태의 변수명은 mainActor
//
// mainActor.울다가_웃다();
// mainActor.빡쳐한다();
주연배우 mainActor = new 이도현();
mainActor.슬픈_연기를_하다();
mainActor.잔잔한_대화를_하다();
mainActor.화를_내다();
}
}
- set은 중복을 제거할때 효과적이다
< 열거 서비스 >
- 쉘(Shell : 껍질) : 운영체제와 사용자 사이에 있는 사용자 인터페이스
- 바탕화면도 윈도우 쉘이다
- 유닉스는 태생이 쉘이라 쉘로 모든걸 다할수가 있다
- 프로그램이 메인에 올라가고 실행될수있는 상태가 되면 그걸 '프로세스'라고 한다
- 하나의 cpu가 여러 프로세스를 한번에 실행할수는 없고 책갈피처럼 실행하다가 중간에 표시해놓고 다음프로세스 실행하고~ 이런식으로 작업을 한다. 이과정을 '시분할'(시간을 나눈다) 이라고 한다
- 웹브라우저 하나가 여러동작을 한번에 할수 있는건 책갈피가 하나이기 때문이다
- 스위칭문제 : 웹브라우저가 자식을 계속 낳으면서 처리속도가 느려지는..
- 프로세스 안에서 다시 시간을 나눠가질수 있는 context : 쓰레드
- 쓰레드로 바꾸면 스위칭 시간이 줄어들게 된다
- 프로세스 올리지말고 함수만 올려서 비동기로 동시에 실행해~ > 이게 쓰레드
package ex9.collection2;
public class ThreadProgram {
public static void print() {
for(int i=0; i<10000; i++)
System.out.println("sub hello"+i);
}
public static void main(String[] args) {
Thread th = new Thread(new Runnable() { //runnable이 쓰레드를 실행시켜준다
@Override
public void run() { //이로써 메인보다 쓰레드쪽이 먼저 실행이됨
//하지만 횟수가 너무 많으면 섞어져서 나오기도함
print();
}
});
th.start();
for(int i=0; i<1000; i++)
System.out.println("main hello"+i);
System.out.println("메인프로세스 종료");
}
}
- 이터레이터로 인덱스객체를 호출한다
- 이터레이터를 이용한 열거서비스를 사용한다
package ex9.collection2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ThreadProgram {
static List<Integer> list;
//전역블록(초기화라서 무조건 해줘야함)
static {
list = new ArrayList<>();
for(int i=0; i<1000; i++)
list.add(i+1);
}
public static void main(String[] args) {
Thread th = new Thread(()-> {
Iterator<Integer> it = list.iterator();
while(it.hasNext())
System.out.println("sub hello"+it.next());
});
th.start();
Iterator<Integer> it = list.iterator();
while(it.hasNext())
System.out.println("main hello"+it.next());
System.out.println("메인 프로세스 종료");
}
}
- 인덱스에 접근하기 위해서는 무조건 이터레이터를 써야한다
- 이터레이터를 반환해주는 객체 '이터러블한 객체' > 그게 for each 문이다(각각의 값을 하나하나 꺼내서 열거해주는)
public static void main(String[] args) {
Thread th = new Thread(()-> {
// Iterator<Integer> it = list.iterator();
// while(it.hasNext())
// System.out.println("sub hello"+it.next());
//while문 쓸 필요 없이 이렇게 for-each문으로 배열값 꺼내서 볼수있다
for(Integer n : list)
System.out.println(n);
});
- 이터레이터가 next()와 hasnext()를 갖고있다
- 단순하게 이터러블 함수만 갖고 있으면 안된다
- 이렇게 구현하면...안된대요!!!▼
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Integer next() {
// TODO Auto-generated method stub
return null;
}
};
}
- 얘는 this를 두개 가진다. outer의 this와 이터레이터 객체 안쪽의 this
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private int index = 0;
@Override
public boolean hasNext() {
return false;
}
@Override
public Integer next() {
return Exam.this.kor;
//그래서 아우터 이름을 옆에 적어줘야함
}
};
}
- 이렇게 이너클래스를 만들어서 할수도 있다
class ExamIterator implements Iterator<Integer>{
//이너클래스(중첩클래스)
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Integer next() {
// TODO Auto-generated method stub
return null;
}
}
- 인스턴스/static 중첩클래스가 있다
- 반드시 outer의 객체를 통해서 new를 해야함(static이 없으면)
package ex9.collection2;
import java.util.ArrayList;
import java.util.List;
public class ThreadProgram {
static List<Integer> list;
//전역블록
static {
list = new ArrayList<>();
for(int i=0; i<1000; i++)
list.add(i+1);
}
public static void main(String[] args) {
Exam exam = new Exam(10,20,30);
for(int n: exam)//exam이 이터러블한 애만 받음
System.out.println(n);
Thread th = new Thread(()-> {
// Iterator<Integer> it = list.iterator();
// while(it.hasNext())
// System.out.println("sub hello"+it.next());
for(Integer n : list)
System.out.println(n);
});
// th.start();
// Iterator<Integer> it = list.iterator();
// while(it.hasNext())
// System.out.println("main hello"+it.next());
// System.out.println("메인 프로세스 종료");
System.out.println("dddd");
}
}
package ex9.collection2;
import java.util.Iterator;
public class Exam implements Iterable<Integer>{
private int kor;
private int eng;
private int math;
public Exam() {
}
public Exam(int kor, int eng, int math) {
super();
this.kor = kor;
this.eng = eng;
this.math = math;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
@Override
public String toString() {
return "Exam [kor=" + kor + ", eng=" + eng + ", math=" + math + "]";
}
class ExamIterator implements Iterator<Integer>{
//이게 이너클래스
private int index;
public ExamIterator() {
index = 0;
}
@Override
public boolean hasNext() {
//값이 있는지 없는지 체크만해줘서 반환값이 불리언
if(index==3)
return false;
return true;
}
@Override
public Integer next() {
//얘는 직접적으로 값을 보내줌
//바깥의 this를 사용하는 인스턴스 중첩 클래스
switch(index++) {
case 0:
return Exam.this.kor;
case 1:
return Exam.this.eng;
case 2:
return Exam.this.math;
}
return Exam.this.kor;
}
}
//오버라이드 함수
public Iterator iterator() {
return new ExamIterator();
}//만드는 이유: 클래스 반환
}
'국비수업 > 수업정리' 카테고리의 다른 글
JSP / 2 (0) | 2023.05.23 |
---|---|
JSP (0) | 2023.05.22 |
Stream Api + 컬렉션 / 3 (0) | 2023.05.18 |
인터페이스 / 다형성 / 익명클래스 (0) | 2023.05.17 |
컬렉션 / 2 + 추상화 (0) | 2023.05.16 |