A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 18895700133 中级黑马   /  2016-5-6 22:43  /  346 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

使用Runnable接口创建多线程:

这是一个构造方法Thread(Runnable target)构造方法.Runnable接口中只有一个方法run()方法。当使用Thread(Runnable target)方法创建线程时,需为该方法传递一个实现 了Runnable接口的类对象中的run()方法作为其运行代码而不再调用Thread类中的run方法了


  1. public class ThreadDemo  
  2. {  
  3. public static void main(String args[])  
  4. //new TestThread().start();  
  5. TestThread tt=new TestThread();//创建TestThread类的一个实例  
  6. Thread t= new TestThread();//创建一个Thread类的实例  
  7. t.start();//使线程进入Runnable状态   
  8. while(true)  
  9. {  
  10. System.out.println("main thread is running");  
  11. }  
  12. }  
  13. }  
  14. class TestThread implements Runnable//extends Thread  
  15. {  
  16. public void run()//线程的代码段,当执行start()里,线程开始执行  
  17. {  
  18. while(true)  
  19. {  
  20. System.out,println("Thread.currentThread().getName() is running");  
  21. }  
  22. }  
  23. } /* 何问起 hovertree.com */
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马