java多线程实例

方式一:
public class ThreadTest
{
 
 public static void main(String[] args) throws Exception
 {
  new MyThread().start();
 }
 
}

class MyThread extends Thread
{
 
 public void run()
 {
   
    while(true)
    {
     System.out.println("Thread say:Hello,World!");
     try
     {
   sleep(5000);
     }
     catch (InterruptedException e)
     {
     }
    }
    }
}

方式二:
public class ThreadTest2
{
    public static void main(String[] args)
    {
        Thread t1=new Thread(new MyThreadRun());
        Thread t2=new Thread(new MyThreadRun());
     
        t1.setName("t1");
        t2.setName("t2");
       
        t1.start();
        t2.start();
    }
}

class MyThreadRun implements Runnable
{
 public void run()
    {
        System.out.println(Thread.currentThread().getName());
    }
}

posted @ 2012-09-01 15:06  嗨,你的益达~~~  阅读(124)  评论(0)    收藏  举报