Study/Java

스레드

Houkibosi 2012. 10. 22. 14:52

스레드

- 스레드의 개요

- 스레드의 생성과 실행

- 스레드 상태

- 스레드의 스케줄링

- 스레드간의 조정

 

* 멀티 태스킹

여러개의 애플리케이션을 동시에 실행하여서 컴퓨터 시스템의 성능을 높이기 위한 기법

 

스레드와 프로세스의 결정적인 차이점?

 

스레드를 사용해야만 하는 프로그램을 생각하여 보자!

 

 

쓰레드 예제

import java.*;
import java.io.IOException;

class MyThread extends Thread
{
 public void run()
 {
  for(int i = 0; i < 10; i++)
  {
   System.out.print((i+1) + "");
  }   
 }
}

class MyRunnable implements Runnable
{
 String Name;
 
 public MyRunnable(){ Name = ""; }
 public MyRunnable(String name){ Name = name; }
 
 public void run()
 {
  for(int i =0; i<10; i++)
   System.out.print(Name + i + " ");   
 }
}

public class console

 public static void main(String[] args) throws Exception
 {
  //Thread t = new MyThread();
  Thread t2 = new Thread(new MyRunnable("A"));
  Thread t3 = new Thread(new MyRunnable("B"));
 


  //t.start();  
  t2.start();
  t3.start();
 }

 
 
 public static void sub()
 {
  
 }

}

 

 

 

 

스레드 심심풀이 예제

import java.*;
import java.io.IOException;

class MyThread extends Thread
{
 public void run()
 {
  for (int i = 0; i < 10; i++)
  {
   System.out.print((i + 1) + "");
  }
 }
}

class MyRunnable implements Runnable
{
 String Name;

 public MyRunnable()
 {
  Name = "";
 }

 public MyRunnable(String name)
 {
  Name = name;
 }

 public void run()
 {
  try
  {
   for (int i = 0; ; i++)
   {
    String test = "Hello World!!";
    
    //System.out.print(Name + i + " ");
    System.out.print(test.charAt(i));
    Thread.sleep(500);
    
    if(test.length() == (i+1))
     break;
   }
  } catch (Exception e)
  {
   System.out.print(" Error ");
  }
 }
}

public class console
{
 public static void main(String[] args) throws Exception
 {
  // Thread t = new MyThread();
  Thread t2 = new Thread(new MyRunnable("A"));
  //Thread t3 = new Thread(new MyRunnable("B"));

  // t.start();
  t2.start();
  //t3.start();
 }

 public static void sub()
 {

 }

}

 

 

 

스레드 예제 최종

import java.*;
import java.io.IOException;

class MyThread extends Thread
{
 public void run()
 {
  for (int i = 0; i < 10; i++)
  {
   System.out.print((i + 1) + "");
  }
 }
}

class MyRunnable implements Runnable
{
 String Name;

 public MyRunnable()
 {
  Name = "";
 }

 public MyRunnable(String name)
 {
  Name = name;
 }

 public void run()
 {
  try
  {
   for (int i = 0;; i++)
   {
    String test = "Hello World!!";

    // System.out.print(Name + i + " ");
    System.out.print(test.charAt(i));
    Thread.sleep(500);

    if (test.length() == (i + 1))
     break;
   }
  } catch (Exception e)
  {
   System.out.print(" Error ");
  }
 }
}

class MessageLoop implements Runnable
{
 public void run()
 {
  String message[] =
  { "Pride will have a fall.",
    "Power is dangerous unless you have humility.",
    "Office changes manners.", "Empty vessels make the most sound." };
  try
  {
   for (int i = 0; i < message.length; i++)
   {
    System.out.println(message[i]);
    Thread.sleep(1000);
   }
  } catch (Exception e)
  {

  }
 }
}

public class console
{
 public static void main(String[] args) throws Exception
 {
  // Thread t = new MyThread();
  // Thread t2 = new Thread(new MyRunnable("A"));
  // Thread t3 = new Thread(new MyRunnable("B"));
  Thread t4 = new Thread(new MessageLoop());

  // t.start();
  // t2.start();
  // t3.start();
  t4.start();
 }

 void print(String message)
 {
  String threadName = Thread.currentThread().getName();
  System.out.format("%s: %s%n", threadName, message);
 }

}