线程学习二 ----通过实现Runnable接口

来源:互联网 发布:mac qq五笔输入法 编辑:程序博客网 时间:2024/06/14 02:55

实现Runnable接口,需履盖run()

 

public class SecondThread implements Runnable
{
 public static void main(String[] args)
 {
  SecondThread st=new SecondThread();
  Thread t=new Thread(st);//在此处与继承Thread不同,
  t.start();
  print("main");
 }

 public void run(){
  print("thread");
 }

 public static void print(String s)
 {
  try
  {
   for (int i=0;i<10 ;i++ )
   {
    Thread.sleep(1000);//暂停一秒钟
    System.out.println(s+i);
   }
  }catch(Exception e){}
 }

}

原创粉丝点击