JAVA 多线程和并发学习笔记(二)

1、Java中建立线程方法java

1. 继承Thread类建立线程类
  定义Thread类的子类,重写该类的run()方法。该方法为线程执行体。 建立Thread子类的实例。即线程对象。 调用线程对象的start()方法启动该线程,示例代码以下:编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  class  ThreadTest extends Thread{ 
     int  i = 0; 
     //重写run方法,run方法的方法体就是现场执行体 
     public  void  run() { 
         for (;i<10;i++){ 
            System. out .println(i); 
       
    
     public  static  void  main(String[] args) { 
        for ( int  i = 0;i< 10;i++)  { 
             System. out .println(Thread.currentThread().getName()+ "  : " +i); 
             new  ThreadTest().start(); 
        
    }  

  

1
2
3
4
5
6
7
8
// 或者   
public  static  void  main(String[] args) { 
        for ( int  i = 0;i< 10;i++)  {  
          new  Thread(){ public  void  run() {
              System. out .println(Thread.currentThread().getName());
         }.start();
        
     }  

  

2. 实现Runnable接口建立线程类
  定义Runnable接口的实现类,重写该接口的run()方法。该方法为线程执行体。 建立Runnable实现类的实例。并以此实例做为Thread的target来建立Thread对象。该Thread对象才是真正的线程对象。 调用线程对象(该Thread对象)的start()方法启动该线程。多线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public  class  RunnableThreadTest implements Runnable  { 
  public  void  run()    { 
        for ( int  i = 0;i <10;i++)   { 
             System. out .println(Thread.currentThread().getName()+ " " +i); 
        
    
     public  static  void  main(String[] args)   { 
        for ( int  i = 0;i < 100;i++)    { 
            System. out .println(Thread.currentThread().getName()+ " " +i); 
               RunnableThreadTest rtt =  new  RunnableThreadTest(); 
              new  Thread(rtt, "新线程1" ).start();  
        }  
     }   

  

1
2
3
4
5
6
7
8
9
10
// 或者
     public  static  void  main(String[] args)   { 
        for ( int  i = 0;i < 100;i++)    {         
             new  Thread(  new  Runnable() {
                 public  void  run() {
                      System. out .println(Thread.currentThread().getName()); 
                 }
                }).start();
          }  
     }

  

3. 使用Callable和Future建立线程并发

  • 建立Callable接口的实现类,并实现call()方法,该call()方法将做为线程执行体,而且有返回值。
  • 建立Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callable对象的call()方法的返回值。
  • 使用FutureTask对象做为Thread对象的target建立并启动新线程。
  • 调用FutureTask对象的get()方法来得到子线程执行结束后的返回值

示例代码以下:ide

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

2、建立线程的三种方式的对比this

三种方法建立线程各有优劣spa

  1.采用实现Runnable、Callable接口的方式创见多线程.net

  优点:线程

  • 线程类只是实现了Runnable接口或Callable接口,还能够继承其余类。
  • 在这种方式下,多个线程能够共享同一个target对象,因此很是适合多个相同线程来处理同一份资源的状况,从而能够将CPU、代码和数据分开,造成清晰的模型,较好地体现了面向对象的思想。

劣势:code

  •   编程稍微复杂,若是要访问当前线程,则必须使用Thread.currentThread()方法。


 2.使用继承Thread类的方式建立多线程

优点:

  •   编写简单,若是须要访问当前线程,则无需使用Thread.currentThread()方法,直接使用this便可得到当前线程。

劣势:

  • 线程类已经继承了Thread类,因此不能再继承其余父类。

 

参考文章:

1.  java 并发

2.  java多线程与并发之java并发编程实践

3. IBM java 并发专题

相关文章
相关标签/搜索