java创建线程的四种方式

java创建线程的三种方式

1. 继承Thread类创建线程类

[java] view plain copy

  1. package com.thread;    
  2.     
  3. public class FirstThreadTest extends Thread{    
  4.     int i = 0;    
  5.     //重写run方法,run方法的方法体就是现场执行体    
  6.     public void run()    
  7.     {    
  8.         for(;i<100;i++){    
  9.         System.out.println(getName()+"  "+i);    
  10.             
  11.         }    
  12.     }    
  13.     public static void main(String[] args)    
  14.     {    
  15.         for(int i = 0;i< 100;i++)    
  16.         {    
  17.             System.out.println(Thread.currentThread().getName()+"  : "+i);    
  18.             if(i==20)    
  19.             {    
  20.                 new FirstThreadTest().run();    
  21.                 new FirstThreadTest().run();    
  22.             }    
  23.         }    
  24.     }    
  25.     
  26. }    


 

2. 通过Runable接口创建线程类

[java] view plain copy

  1. package com.thread;    
  2.     
  3. public class RunnableThreadTest implements Runnable    
  4. {    
  5.     
  6.     private int i;    
  7.     public void run()    
  8.     {    
  9.         for(i = 0;i <100;i++)    
  10.         {    
  11.             System.out.println(Thread.currentThread().getName()+" "+i);    
  12.         }    
  13.     }    
  14.     public static void main(String[] args)    
  15.     {    
  16.         for(int i = 0;i < 100;i++)    
  17.         {    
  18.             System.out.println(Thread.currentThread().getName()+" "+i);    
  19.             if(i==20)    
  20.             {    
  21.                 RunnableThreadTest rtt = new RunnableThreadTest();    
  22.                 new Thread(rtt,"新线程1").start();    
  23.                 new Thread(rtt,"新线程2").start();    
  24.             }    
  25.         }    
  26.     
  27.     }    
  28.     
  29. }    


 

3. 通过Callable和FutureTask创建线程

    a. 创建Callable接口的实现类,并实现call()方法;
    b. 创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callback对象的call()方法的返回值;
    c. 使用FutureTask对象作为Thread对象的target创建并启动新线程;

    d. 调用FutureTask对象的get()方法来获得子线程执行结束后的返回值。

[java] view plain copy

  1. package com.demo;  
  2.   
  3. import java.util.concurrent.Callable;    
  4. import java.util.concurrent.ExecutionException;    
  5. import java.util.concurrent.FutureTask;    
  6.     
  7. public class CallableThreadTest implements Callable<Integer>    
  8. {    
  9.     
  10.     public static void main(String[] args)    
  11.     {    
  12.         CallableThreadTest ctt = new CallableThreadTest();    
  13.         FutureTask<Integer> ft = new FutureTask<Integer>(ctt);    
  14. //        Thread thread = new Thread(ft,"有返回值的线程");  
  15. //        thread.start();  
  16.         for(int i = 0;i < 100;i++)    
  17.         {    
  18.             System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i);    
  19.             if(i==20)    
  20.             {    
  21.                 new Thread(ft,"有返回值的线程").start();    
  22.             }    
  23.         }    
  24.         try    
  25.         {    
  26.             System.out.println("子线程的返回值:"+ft.get());    
  27.         } catch (InterruptedException e)    
  28.         {    
  29.             e.printStackTrace();    
  30.         } catch (ExecutionException e)    
  31.         {    
  32.             e.printStackTrace();    
  33.         }    
  34.     
  35.     }    
  36.     
  37.     @Override    
  38.     public Integer call() throws Exception    
  39.     {    
  40.         int i = 0;    
  41.         for(;i<100;i++)    
  42.         {    
  43.             System.out.println(Thread.currentThread().getName()+" "+i);    
  44.         }    
  45.         return i;    
  46.     }    
  47.     
  48. }    

4. 通过线程池创建线程

[java] view plain copy

  1. /** 
  2.  *  
  3.  */  
  4. package com.demo;  
  5.   
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8.   
  9. /** 
  10.  * @author Maggie 
  11.  * 
  12.  */  
  13. public class ThreadPool   
  14. {  
  15.     /* POOL_NUM */  
  16.     private static int POOL_NUM = 10;  
  17.       
  18.     /** 
  19.      * Main function 
  20.      */  
  21.     public static void main(String[] args)  
  22.     {  
  23.         ExecutorService executorService = Executors.newFixedThreadPool(5);  
  24.         for(int i = 0; i<POOL_NUM; i++)  
  25.         {  
  26.             RunnableThread thread = new RunnableThread();  
  27.             executorService.execute(thread);  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. class RunnableThread implements Runnable  
  33. {  
  34.     private int THREAD_NUM = 10;  
  35.     public void run()  
  36.     {  
  37.         for(int i = 0; i<THREAD_NUM; i++)  
  38.         {  
  39.             System.out.println("线程" + Thread.currentThread() + " " + i);  
  40.         }   
  41.     }  
  42. }  

猜你喜欢

转载自my.oschina.net/u/3787897/blog/1630880