자바 87) 쓰레드 2 런에이블

hahahoho셍·2021년 5월 15일
0

자바 공부

목록 보기
88/97
package ex13_thread;
//스레드 만들기 2

class Thread5 implements Runnable{

	@Override
	public void run() {
		for (int i=0; i<10;i++) {
			System.out.println("스레드 오번 : "+i);
		}
	}
}


public class J20210507_02_Runnable {

	public static void main(String[] args) {
		Thread5 t5 = new Thread5(); //실행 구현 (런을 먼저 만들고)
		Thread th = new Thread(t5); //스레드 생성 (만든 런을 스레드에 전달)
		//th.start();  //스레드 시작
		
		//익명 클래스
//		Runnable r1 = new Runnable() {
//			
//			@Override
//			public void run() {
//				for (int i=0; i<10;i++) {
//					System.out.println("익명 스레드 : "+i);
//				}
//				
//			}
//		};
//		Thread th2 = new Thread(r1);
//		th2.start();
		
		//익명스레드2 (단축)
		Thread th3 = new Thread (new Runnable() {  //인터페이스를 스레드 생성 괄호안에서 만들어버림
			
			@Override
			public void run() {
				for (int i=0; i<30;i++) {
					System.out.println("익명 스레드2 : "+i);
				}
				
			}
		});
		//스레드 우선순위 : 1~10 (기본값 5)
		th3.setPriority(10);
		th3.start();
		
		
		//익명스레드2 (실습)
		Thread th4 = new Thread (new Runnable() {
			
			@Override
			public void run() {
				for (int i=0; i<30;i++) {
					System.out.println("슈퍼 익명 스레드2 : "+i);
				}
				
			}
		});
		//스레드 우선순위 : 1~10 (기본값 5)
		th4.setPriority(1);  //우선순위 정하기 (우선순위가 클수록 높음)
		th4.start();
		
		
		
		//메인 포문 (비교용)
		for (int i=0; i<10;i++) {
			System.out.println("메인 : "+i);
		}
		
	}

}
profile
그냥 공부 한거 적는 벨로그 하하하핳ㅎ하하하핳하ㅏ

0개의 댓글

관련 채용 정보