package ch01;
import javax.swing.JFrame;
class MyRunnable2 extends JFrame{
int grade = 10;
//내부 익명 객체를 변수에 담아서 사용 하는 방법 ***실무****
Runnable runable = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.print("-");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}0.5초 예외상황이 발생
Thread.sleep(500); 하게되면 쓰레드르 잠들게 했을때
못깨어나는 경우가 생김(화면이멈추는 등)
InterruptedException 이것을 띄워 재실행
}
}
};안드로이드나 스프링에서 자주사용
맴버변수를 초기화하는 과정을 거칠때 객체를 만들어 메모리 힙영역에올리고
힙영역에 객체가 만들어졋으면 그 주소값을 runable자리에 삽입
변수에 주소값을 넣는다
public MyRunnable2() {
// runable.run(); //맴버 변수로 접근해서 run메서드 호출
}//가장 먼저 실행하게 하려면 생성자를 쓰는 것이 좋다
}
public class RunnableTest2 {
public static void main(String[] args) {
MyRunnable2 myRunnable2 =new MyRunnable2();
외부에서 동작시켜보기
myRunnable2.runable.run(); //runable.run();과 같음
}
sleep ->시간이 지나면
wait를 하면 강제로 runable > notify() 사용하면 돌려놓는다
join 쓰레드 두개가 있을 때 이 쓰레드에서 어떠한 동작에 대한 결과값을 받아야
정확한 동작을 할 경우 join -> other thread exits
}