# Thread SCJP

class ThreadEx1{
 public static void main(String[] args)  {
  ThreadEx1_1 t1 = new ThreadEx1_1();
  Runnable r = new ThreadEx1_2();
  Thread t2 = new Thread(r); // 생성자 Thread(Runnable target)
  t1.start();
  t2.start();
 }
}

class ThreadEx1_1 extends Thread{
 public void run(){
  for(int i = 0;i < 5;i++){
   System.out.println(getName()); // 조상인 Thread의 getName() 호출
  }
 }
}

class ThreadEx1_2 implements Runnable{
 public void run(){
  for(int i = 0;i < 5;i++){
   // Thread.currentThread() 현재 실행중인 Thread를 반환
   System.out.println(Thread.currentThread().getName());
  }
 }
}

// Thread 1개로 2개의 동작

class ThreadEx4{
 public static void main(String[] args)  {
  long startTime = System.currentTimeMillis();
  for(int i = 0;i < 300;i++){
   System.out.print(" - ");
  }
  System.out.print("소요시간 1 : "+(System.currentTimeMillis() - startTime));

  for(int i = 0;i < 300;i++){
   System.out.print("|");
  }
  System.out.print("소요시간 2 : " +(System.currentTimeMillis() - startTime));
 }
}

// Thread 2개로 2개의 동작

class ThreadEx5{
 static long startTime = 0;
 public static void main(String[] args)  {
  ThreadEx5_1 th1 = new ThreadEx5_1();
  th1.start();
  startTime = System.currentTimeMillis();
  for(int i = 0;i < 300;i++){
   System.out.print("-");
  }
  System.out.print("소요시간1 : " + (System.currentTimeMillis() - ThreadEx5.startTime));
 }
}

class ThreadEx5_1 extends Thread{
 public void run(){
  for(int i = 0;i < 300;i++){
   System.out.print("|");
  }
  System.out.print("소요시간 2 : " + (System.currentTimeMillis() - ThreadEx5.startTime));
 }
}


import javax.swing.JOptionPane;

class  ThreadEx6{
 public static void main(String[] args) throws Exception{
  String input = JOptionPane.showInputDialog("아무값이나 입력하세요");

  System.out.println("입력하신 값은 " +input+ "입니다");
  for(int i = 10;i > 0;i--){
   System.out.println(i);
   try{
    Thread.sleep(1000);
    //sleep - 스레드를 일시정지 (매개변수 ms)
   }catch(Exception e){}
  }
 }
}

Multi thread

import javax.swing.JOptionPane;

class ThreadEx7{
 public static void main(String[] args) throws Exception {
  ThreadEx7_1 th1 = new ThreadEx7_1();
  th1.start();
  String input = JOptionPane.showInputDialog("아무값이나 입력하세요");
  System.out.println("입력하신 값은 " + input + " 입니다");
 }
}

class ThreadEx7_1 extends Thread{
 public void run(){
  for(int i = 10;i > 0;i--){
   System.out.println(i);
   try{
    sleep(1000);
   }catch(Exception e){}
  }
 }
}

쓰레드 우선순위
- 우선순위에 따라 쓰레드의 실행시점을 달리 할 수 있다.
- 우선순위 범위 (1~10)
- 우선순위 숫자가 높을수록 먼저 실행된다.
- 기본우선순위는 (5) main() 는 기본이 5로 지정.

class ThreadEx9{
 public static void main(String[] args)  {
  ThreadEx9_1 th1 = new ThreadEx9_1();
  ThreadEx9_2 th2 = new ThreadEx9_2();
  th2.setPriority(7);
  System.out.println("Priority of th1(-) : " +th1.getPriority());
  System.out.println("Priority of th2(|) : " +th2.getPriority());
  th1.start();
  th2.start();
 }
}

class ThreadEx9_1 extends Thread{
 public void run(){
  for(int i = 0;i < 300;i++){
   System.out.print("-");
   for(int x = 0;x < 10000000;x++);
  }
 }
}

class ThreadEx9_2 extends Thread{
 public void run(){
  for(int i = 0;i < 300;i++){
   System.out.print("|");
   for(int x = 0;x < 10000000;x++);
  }
 }
}

쓰레드 그룹
- 서로 관련된 쓰레드는 그룹으로 다룰수 있다.
- 보안상의 이유로 도입된 개념
- ThreadGroup class 사용
- 쓰레드를 그룹에 포함시킬때 생성자를 이용한다.

class ThreadEx10{
 public static void main(String[] args)  {
  ThreadGroup main = Thread.currentThread().getThreadGroup();
  ThreadGroup grp1 = new ThreadGroup("Group1");
  ThreadGroup grp2 = new ThreadGroup("Group2");
  ThreadGroup subGrp1 = new ThreadGroup(grp1, "SubGroup1");
  grp1.setMaxPriority(3);
  Thread th1 = new Thread(grp1, "th1");
  Thread th2 = new Thread(subGrp1, "th2");
  Thread th3 = new Thread(grp2, "th3");
  th1.start();
  th2.start();
  th3.start();
  System.out.println(">>List of ThreadGroup : " +main.getName()
         +", Active ThreadGroup : " +main.activeGroupCount()
         +", Active Thread : " +main.activeCount());
  main.list();
 }
}

데몬 쓰레드
- 일반 쓰레드의 작업을 돕기위해서 만든다.
- 일반 쓰레드 내부에서 일반 쓰레드와 함께 동작한다.
- 일반 스레드가 종료되면 데몬 쓰레드는 강제 종료된다.
- setDemon() 를 이용해서 start() 보다 먼저 실행되어야 한다.

class ThreadEx11 implements Runnable{
 static boolean autoSave = false;
 public static void main(String[] args){
  Thread t = new Thread(new ThreadEx11());
  t.setDaemon(true);
  t.start();

  for(int i = 1;i <= 20;i++){
   try{
    Thread.sleep(1000);
   }catch(InterruptedException e){}
   System.out.println(i);
   if(i == 5)
    autoSave = true;
  }
  System.out.println("Program Exit");
 }

 public void run(){
  while(true){
   try{
    Thread.sleep(3 * 1000);
   }catch(InterruptedException e){}
   if(autoSave){
    autoSave();
   }
  }
 }

 public void autoSave(){
  System.out.println("Auto save");
 }
}


쓰레드 실행제어
- 실행중인 쓰레드를 필요에 따라, 일시정지, 멈춤, 다시 실행 등의 메서드를 이용해서 제어할 수 있다.

class ThreadEx16{
 public static void main(String args[]){
  RunImplEx16 r = new RunImplEx16();
  Thread th1 = new Thread(r, "*");
  Thread th2 = new Thread(r, "**");
  Thread th3 = new Thread(r, "***");
  th1.start();
  th2.start();
  th3.start();

  try{
   Thread.sleep(2000);
   th1.suspend();
   Thread.sleep(2000);
   th2.suspend();
   Thread.sleep(3000);
   th1.resume();
   Thread.sleep(3000);
   th1.stop();
   th2.stop();
   Thread.sleep(2000);
   th3.stop();
  }catch(InterruptedException e){}
 }
}

class RunImplEx16 implements Runnable{
 public void run(){
  while(true){
   System.out.println(Thread.currentThread().getName());
   try{
    Thread.sleep(1000);
   }catch(InterruptedException e){}
  }
 }




1 2 3 4 5 6 7 8 9 10 다음