最简单的java多线程代码(重写thread或者runnable的run方法)

 

http://blog.csdn.net/testcs_dn/article/details/42526549

java线程使用示例——最简单的线程

线程使用示例一:

 

[java] view plain copy
 
  1. public class ThreadTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         //线程使用示例一:  
  5.         new Thread() {  
  6.             public void run() {  
  7.                 while (true) {  
  8.                     try {  
  9.                         System.out.println("线程输出");  
  10.                           
  11.                         //休眠两秒  
  12.                         Thread.sleep(2 * 1000);  
  13.                     } catch (InterruptedException e) {  
  14.                         e.printStackTrace();  
  15.                     }  
  16.                 }  
  17.             };  
  18.         }.start();  
  19.     }  
  20.   
  21. }  

线程使用示例二:

[java] view plain copy
 
  1. //线程使用示例二:  
  2. Thread t1 = new Thread(new Runnable(){  
  3.     public void run(){  
  4.         System.out.println("Mythread 线程t1");  
  5.         while(true){  
  6.             Singleton3 single3 = Singleton3.getInstance(null);  
  7.             System.out.println("线程t1 >> " + single3.toString());  
  8.             try {  
  9.                 Thread.sleep(2000);  
  10.             } catch (InterruptedException e) {  
  11.                 // TODO Auto-generated catch block  
  12.                 e.printStackTrace();  
  13.             }  
  14.         }  
  15.           
  16.     }  
  17. });  
  18. t1.start();  
posted @ 2017-11-30 20:19  蓝色星辰  阅读(7140)  评论(0编辑  收藏  举报