class ThreadWithClass extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(getName()); // 스레드 이름 출력
try {
Thread.sleep(10); // 10ms동안 스레드 정지
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadWithRunnable implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread01 {
public static void main(String[] args){
ThreadWithClass thread1 = new ThreadWithClass(); // Thread 클래스를 상속받는 방법
Thread thread2 = new Thread(new ThreadWithRunnable()); // Runnable 인터페이스를 구현하는 방법
thread2.setPriority(10); // 스레드의 우선순위 설정
thread1.start();
thread2.start();
}
}
class ThreadWithRunnable implements Runnable {
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Thread03 {
public static void main(String[] args){
Thread thread0 = new Thread(new ThreadWithRunnable());
thread0.start();
System.out.println(thread0.getThreadGroup()); // java.lang.ThreadGroup[name=main,maxpri=10]
ThreadGroup group = new ThreadGroup("myThread");
group.setMaxPriority(7); // 최대 우선순위 설정
// 스레드 그룹을 지정해 새로운 스레드 생성
Thread thread1 = new Thread(group, new ThreadWithRunnable());
thread1.start();
System.out.println(thread1.getThreadGroup()); // java.lang.ThreadGroup[name=myThread,maxpri=7]
}
}
출처:
tcpschool.com/java/java_thread_concept