본문 바로가기
프로그램/Java 1000제

Java 21제] 1~10까지 출력하시오.(Thread 활용)

by 건티 2022. 12. 8.
728x90

출처 : 반크_세계유산 경복궁

 

참고풀이]

import java.lang.Thread;

public class OAT_Thread extends Thread {
   public void run()
   {
      try {
         for(int i=1;i<=10;i++)
         {
            if(i%2==0) System.out.printf("%3d",i);
            Thread.sleep(1000);//1초를 쉰다.
         }
      }catch(InterruptedException e) {
         System.out.println(e.getMessage());
      }
   }

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      OAT_Thread oat = new OAT_Thread();

      try {
         oat.start();
         for(int i=1;i<=10;i++)
         {
            if(i%2==1) System.out.printf("%3d",i);
            Thread.sleep(1000);//1초를 쉰다.
         }
         System.out.println();
      }catch(InterruptedException e) {
         System.out.println(e.getMessage());
      }
   }
}

 

참고풀이 결과]

 

 

 

 

 

대한민국의 아름다운 영토, 독도

 

반응형

댓글