티스토리 뷰

반응형

//Main Thread 제어 예제

import java.time.*;


public class tthread{


public static void main(String[] args){

for(int i = 0; i < 5; i++){

System.out.println("ㄸㄸㄸ   " + LocalDateTime.now());

}

for(int j = 0; j < 5; j++){

System.out.println("ㄸㄷ~ " + LocalDateTime.now());

try{Thread.sleep(1000);} catch(Exception e){}

}

for(int k = 0; k <  5; k++){

System.out.println("ㄴㄳㅇ  " + LocalDateTime.now());

try { Thread.sleep(500); } catch(Exception e) {}

}

}

}



//Thread 생성 예제-Runnable Interface 사용

import java.time.*;


public class tthread2 {

public static void main(String[] args) {

Runnable threadTask = new ThreadTask();

Thread thread = new Thread(threadTask);

thread.start();

for(int i = 0 ; i < 5; i++) {

System.out.println("자바가" + LocalDateTime.now());

try {Thread.sleep(500);} catch(Exception e) {}

}

}


}


class ThreadTask implements Runnable{

public void run() {

for(int i = 0; i < 5; i ++) {

System.out.println("어려워요 " + LocalDateTime.now());

try {Thread.sleep(500);} catch(Exception e) {}

}

}

}




//Thread 생성 예제-Thread 하위 클래스로부터 생성 

import java.time.*;


public class tthread3{

public static void main(String[] args) {

Thread thread = new taskThread();

thread.start();

for(int i = 0 ; i < 5; i++) {

System.out.println("제일 어려워요 " + LocalDateTime.now()) ;

try {Thread.sleep(500);} catch(Exception e) {}

}

}

}


class taskThread extends Thread{

@Override

public void run() {

for(int i = 0; i < 5; i++) {

System.out.println("자바가 " + LocalDateTime.now());

try {Thread.sleep(500);}catch(Exception e) {}

}

}

}

댓글

티스토리 방명록

최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday