람다식 예제(현재시각 스레드)

·2022년 11월 5일
0

람다식으로 1초마다 시간 출력(Runnable 이용)

0.사용할 시간 클래스

LocalDateTime(시각.now()), DateTimeFormatter(표현형식 만들기.ofPattern())

1.큰 틀 만들기

public class Test02_day18 {
	public static void main(String[] args) {
		//1.익명의 내부클래스
		Runnable r = new Runnable() {
			@Override
			public void run() {
			}
		};
		//2. 람다식
		new Thread(()->{
			
		}).start();

	}
}

2.구현부 작성

public class Test02_day18 {
	public static void main(String[] args) {
		//1.익명의 내부클래스
		Runnable r = new Runnable() {
			@Override
			public void run() {
				while(true) {
					LocalDateTime lt = LocalDateTime.now();
					DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-mm-dd HH:mm:ss");
					String time = lt.format(df);
					System.out.println("현재시각: "+time);
					try {
						Thread.sleep(1000);
					}catch(InterruptedException ie) {
						System.out.println(ie.getMessage());
					}
				}	
			}
		};
		new Thread(r).start();
		//2. 람다식
		new Thread(()->{
			while(true) {
				LocalDateTime lt = LocalDateTime.now();
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yy년 mm월 dd일 hh시 mm분 ss초");
				String time= lt.format(df);
				System.out.println("지금은 "+time+"입니다.");
				try {
					Thread.sleep(1000);
				}catch(InterruptedException ie) {
					System.out.println(ie.getMessage());
				}
			}
		}).start();

	}
}
profile
웹개발입문자

0개의 댓글