10.쓰레드

MultiThreadEx.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MultiThreadEx {

public static void main(String[] args) {
Thread thread1 = new AlphabetThread(); //쓰레드 생성방법1
Thread thread2 = new Thread( new DigitThread()); //쓰레드 생성방법2
//매개변수로 Runnable이 구현된 객체를 받음

new Thread( new Runnable() { //쓰레드 생성방법3
@Override
public void run() {
for( char c = 'A'; c<= 'z'; c++) {
System.out.print( c );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();;

thread1.start();
thread2.start();
}
}
DigitThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
public class DigitThread implements Runnable{
public void run() {
for(int i = 0; i < 10; i++) {
System.out.print( i );
try {
Thread.sleep( 1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

AlphabetThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class AlphabetThread extends Thread{
@Override
public void run() {
for( char c = 'a'; c <= 'z'; c++) {
System.out.print( c );
try {
Thread.sleep( 1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Share