기록

*26일차 실습 본문

학원/실습

*26일차 실습

pringspring 2022. 3. 2. 10:55

[Thread 실습문제]
<문제 1> 
 - 실행용클래스 com.thread.sleep.SleepTest
 + sendAphorism() : void 

 - 좋아하는 속담, 경구 10개를 문자열배열에 담고, 3초마다 랜덤하게 출력하세요.
 - 10번 출력후 종료하세요.

package com.thread.sleep;

public class SleepTest implements Runnable{

	@Override
	public void run() {
		String[] strarr =new String[10];
		
		strarr[0] = "Regret for wasted time is more wasted time";
		strarr[1] = "connecting the dots";
		strarr[2] = "Love yourself.";
		strarr[3] = "Seize the day";
		strarr[4] = "Don't dwell on the past";
		strarr[5] = "Where there is a will there is a way";
		strarr[6] = "Be brave";
		strarr[7] = "Live positive";
		strarr[8] = "why not me";
		strarr[9] = "Life is all about timing";
		
		for(int i=1 ; i<10 ;i++) {
			System.out.println(strarr[i]);
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

}
------------------------------
package com.thread.sleep;


public class Run {
	public static void main(String[] args) {
		new Thread(new SleepTest()).start();
		
		System.out.println("[" + Thread.currentThread().getName()+"종료 !]");
	}
	
//	public void printTest() {
//		new Thread(new SleepTest()).start();
//		
//	}
}

 




<문제 2>
- 실행용 클래스 : com.thread.alphabet.AlphabetTest.java
- 쓰레드 클래스 : com.thread.alphabet.UpperAlphbetThread.java
               com.thread.alphabet.LowerAlphbetThread.java    
두개의 멀티쓰레드를 만들고, 각각 알파벳대문자, 알파벳 소문자를 출력하세요.

package com.thread.alphabet;

public class AlphabetTest {
	public static void main(String[] args) {

		Thread th1  = new Thread(new UpperAlphabetThread(),"대문자");
		Thread th2  = new Thread(new LowerAlphabetThread(),"소문자");
		
		th1.start();
		th2.start();
	}
}
----------------------------------------
package com.thread.alphabet;

public class UpperAlphabetThread  implements Runnable{


	@Override
	public void run() {
		System.out.println("[대문자 출력]");
		for(char ch='A' ; ch<='Z';ch++) {
			System.out.println(ch);
		}
		
	}

}
----------------------------------------
package com.thread.alphabet;

public class LowerAlphabetThread implements Runnable {

	@Override
	public void run() {
		System.out.println("[소문자 출력]");
		for(char ch='a' ; ch<='z';ch++) {
			System.out.println(ch);
		}
		
	}

}

'학원 > 실습' 카테고리의 다른 글

*35일차 실습  (0) 2022.03.28
*34일차 실습  (0) 2022.03.28
*25일차 실습  (0) 2022.02.28
*24일차 ncs (수강생 평가) -실기  (0) 2022.02.25
*24일차 ncs (수강생 평가) -필기  (0) 2022.02.25