Four ways to create threads in java

Three ways to create threads in java

1. Inherit the Thread class to create a thread class

 

[java] view plain copy

  1. package com.thread;    
  2.     
  3. public class FirstThreadTest extends Thread{    
  4.     int i = 0;    
  5.     //Rewrite the run method, the method body of the run method is the on-site execution body    
  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. Create a thread class through the Runable interface

 

[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,"New thread 1").start();    
  23.                 new Thread(rtt,"New thread 2").start();    
  24.             }    
  25.         }    
  26.     
  27.     }    
  28.     
  29. }    


 

 

3. Create threads through Callable and FutureTask

    a. Create an implementation class of the Callable interface and implement the call() method;
    b. Create an instance of the Callable implementation class, and use the FutureTask class to wrap the Callable object, which encapsulates the return value of the call() method of the Callback object;
    c. Use the FutureTask object as the target of the Thread object to create and start a new thread;

    d. Call the get() method of the FutureTask object to obtain the return value after the execution of the child thread ends.

 

[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, "thread with return value");  
  15. //        thread.start();  
  16.         for(int i = 0;i < 100;i++)    
  17.         {    
  18.             System.out.println(Thread.currentThread().getName()+" The value of the loop variable i"+i);    
  19.             if(i==20)    
  20.             {    
  21.                 new Thread(ft,"thread with return value").start();    
  22.             }    
  23.         }    
  24.         try    
  25.         {    
  26.             System.out.println("Return value of child thread: "+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. Create a thread through the thread pool

[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. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325333930&siteId=291194637