[] STUDY ³»¿ë

ÀÛ¼ºÀÏ 2003-10-21
ÀÛ¼ºÀÚ park
Á¶È¸¼ö 676
Á¦ ¸ñ ¾²·¹µå

¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â

¾²·¹µå


1. ¸ÖƼ ŽºÅ·°ú ¸ÖƼ ¾²·¡µåÀÇ Â÷ÀÌÁ¡

¸ÖƼ ŽºÅ· : Çϳª ÀÌ»óÀÇ ÇÁ·Î¼¼¼­¸¦ ÀÌ¿ëÇÏ¿© ¿©·¯°³ÀÇ ÇÁ·Î±×·¥À» µ¿½Ã¿¡ ½ÇÇàÇÏ´Â °Í.

¸ÖƼ ¾²·¡µå : ÇϳªÀÇ ÇÁ·Î±×·¥¿¡¼­ ¿©·¯°³ÀÇ ÀÏÀ» µ¿½Ã¿¡ ½ÇÇàÇÏ°Ô ÇÏ´Â °Í.

2. ThreadÀÇ »ç¿ë ¼ø¼­

Thread class¸¦ »ó¼ÓÇÑ´Ù

¡é

¾²·¹µå ³»¿ëÀ» run()¸Þ¼Òµå¿¡ ¿À¹ö¶óÀ̵å ÇÑ´Ù.

¡é

ÇØ´ç Ŭ·¡½º¸¦ °´Ã¼·Î »ý¼º

¡é

Ŭ·¡½º °´Ã¼¿¡ start()¸Þ¼­µå È£Ãâ(ÁöÁ¤)


3. Thread¸¦ »ý¼ºÇÏ´Â ¹æ¹ý

1) ´Üµ¶ÀÇ »ó¼ÓÀÏ °æ¿ì

class class_name extends Thread

{

    public void run()

    {

      ¾²·¹µå¿¡¼­ ¼öÇàÇÒ ÇÁ·Î±×·¥ ÀÛ¼º;

    }

    class_name obj = new class_name();

    obj.start();

}


2) ÀÌ¹Ì ´Ù¸¥ Ŭ·¡½º¸¦ »ó¼ÓÇÏ°í ÀÖÀ» °æ¿ì(¿¹ AppletÀ» ÀÌ¹Ì »ó¼ÓÇÏ°í ÀÖÀ» °æ¿ì)

class class_name extends Applet implements Runnable

{

    public void run()

    {

      ¾²·¹µå¿¡¼­ ¼öÇàÇÒ ÇÁ·Î±×·¥ ÀÛ¼º;

    }

    class_name obj = new class_name();

    Thread t=new Thread(obj);

    t.start();

}


3) run() ¸Þ¼Òµå¿Í start() ¸Þ¼ÒµåÀÇ °ü°è

ÇÁ·Î±×·¡¸Ó´Â start()¸¸À» È£ÃâÇÒ ¼ö ÀÖ´Ù. ÇÁ·Î±×·¡¸Ó°¡ start()¸¦ È£ÃâÇϸé ÀÚ¹Ù °¡»ó¸Ó½ÅÀº ÇØ´ç ¾²·¹µå¸¦ Ready »óÅ·Π¹Ù²Ù°í, ¾²·¹µå ½ºÄÉÁì·¯°¡ run()»óÅ·Π¹Ù²Ù¾î ºñ·Î¼Ò run() ¸Þ¼Òµå°¡ ½ÇÇàµÇ´Â °ÍÀÌ´Ù.

4. Thread ¿¹(´Üµ¶»ó¼Ó)

1) ¸Å¼Òµå »ç¿ë¾ÈÇÔ

class ac_Thread_1 extends Thread

{

    int i,sum;

    public void run()

    {

      for(i=0 ; i<11 ; ++i)

      {

        try { Thread.sleep(500);}

        catch(InterruptedException e) { }

        sum=sum+i;

        System.out.println("sum="+sum);

      }

    }

    public static void main(String[] args)

    {

      ac_Thread_1 t1=new ac_Thread_1();

      ac_Thread_1 t2=new ac_Thread_1();

      t1.start();

      t2.start();

    }

}


2) ¸Þ¼Òµå »ç¿ë

class ac_Thread_1 extends Thread

{

    int i, sum;

    public void prn()

    {

      i=i+1;

      sum=sum+i;

      System.out.println("sum="+sum);

    }

    public void run()

    {

      for(int i=0 ; i<11 ; ++i)

      {

        try { Thread.sleep(500);}

        catch(InterruptedException e) { }

        prn();

      }

    }

    public static void main(String[] args)

    {

      ac_Thread_1 t1=new ac_Thread_1();

      ac_Thread_1 t2=new ac_Thread_1();

      t1.start();

      t2.start();

    }

}


3) ¿ÜºÎ Ŭ·¡½º »ç¿ë

class ac_Thread_1 extends Thread

{

    public void run()

    {

      ac_Thread_1_sub ts=new ac_Thread_1_sub();

      for(int i=0 ; i<11 ; ++i)

      {

        try { Thread.sleep(500);}

        catch(InterruptedException e) { }

        ts.prn();

      }

    }

    public static void main(String[] args)

    {

      ac_Thread_1 t1=new ac_Thread_1();

      ac_Thread_1 t2=new ac_Thread_1();

      t1.start();

      t2.start();

      }

    }

class ac_Thread_1_sub

{

    int i,sum;

    void prn()

    {

      i=i+1;

      sum=sum+i;

      System.out.println("sub_sum="+sum);

    }

};


4) ¿ÜºÎ Ŭ·¡½º°¡ Thread¸¦ »ó¼ÓÇÏ¿´À» ¶§

//=========================================================

// Thread - ¿ÜºÎ Ŭ·¡½º°¡ ¾²·¹µåÀÏ °æ¿ì

//---------------------------------------------------------

// Á¦ ¸ñ : Thread »ç¿ëÇϱâ4

// ÀÛ¼ºÀÚ : ¹Ú¿ø±â

// ÀÛ¼ºÀÏ : 2002

//=========================================================

class ac_Thread_4

{

    public static void main(String[] args)

    {

      //Thread t1=new ac_Thread_4_sub1();

      //Thread t2=new ac_Thread_4_sub2();

      ac_Thread_4_sub1 sub_t1=new ac_Thread_4_sub1();

      ac_Thread_4_sub2 sub_t2=new ac_Thread_4_sub2();

      Thread t1=new Thread(sub_t1);

      Thread t2=new Thread(sub_t2);

      t1.start();

      t2.start();

    }

}

class ac_Thread_4_sub1 extends Thread

{

    int i, sum;

    public void run()

    {

      for(i=1 ; i<10 ; i=i+2)

    {

    System.out.println("Thread 1 " + i);

    try

    {

      sleep(500);

    }

    catch (InterruptedException e)

    {

    }

    }

    }

};

class ac_Thread_4_sub2 extends Thread

{

    int i, sum;

    public void run()

    {

      for(i=0 ; i<10 ; i=i+2)

    {

    System.out.println("Thread 2 " + i);

    try

    {

      sleep(500);

    }

    catch (InterruptedException e)

    {

    }

    }

    }

};




5. ´ÙÁß »ó¼ÓÀÏ °æ¿ì(Applet »ç¿ë)

1)

//=========================================================

// Applet

//---------------------------------------------------------

// Á¦ ¸ñ : Thread »ç¿ëÇϱâ2

// ÀÛ¼ºÀÚ : ¹Ú¿ø±â

// ÀÛ¼ºÀÏ : 2002

//=========================================================

import java.awt.*;

import java.applet.*;

public class ap_Thread extends Applet implements Runnable

{

    int i,sum;

    public void init()

    {

      resize(500,500);

      setBackground(Color.white);

    }

    public void start()

    {

      Thread t= new Thread(this);

      t.start();

    }

    public void paint(Graphics g)

    {

      g.drawString("sum="+sum,40,i);

    }

    public void draw(int i)

    {

      Graphics g=getGraphics();

      sum=sum+1;

      g.drawString("sum="+sum,40,i);

    }

    public void run()

    {

    for(i=50; i<400 ; i=i+15)

    {

      //repaint();

      draw(i);

      try

      {

        Thread.sleep(500);

      }

      catch (InterruptedException e)

      {

      }

      }

    }

}

>


¾²·¹µå °ú(¿Í) °ü·ÃµÈ Ãß°¡ Á¤º¸

  • ¿¹¿Üó¸® [2003-10-21] [park´Ô Àç°ø] [Á¶È¸:761]
  • ¾²·¹µå [2003-10-21] [park´Ô Àç°ø] [Á¶È¸:676]

    ¸ñ·ÏÀ¸·Î | ¼öÁ¤ | »èÁ¦ | Á¤º¸Ãß°¡[reply] | ½Å±ÔÀÔ·Â