java多线程实例

本文详细介绍了Java中创建多线程的三种方法:继承Thread类、实现Runnable接口及使用Callable接口配合FutureTask,同时解释了oin线程的概念及用途,并通过示例代码展示其实现过程。

Java对多线程的支持

Java创建多线程的3种常用方法:

    1)继承Thread类

重写Thread类的run方法,创建Thread子类实例,启动线程。

例如:

[java]  view plain copy
  1. /* 
  2.  * @author wxismeit@163.com  wangxu 
  3.  */  
  4. public class TreadOfextends extends Thread{  
  5.     private int i;  
  6.     //重写run()方法  
  7.     public void run(){  
  8.         for(i=0; i<50; i++){  
  9.             System.out.println(getName() + " " + i);  
  10.             //继承Thread类时直接使用this即可获取当前线程  
  11.         }  
  12.     }  
  13.   
  14.     public static void main(String[] args) {  
  15.         System.out.println(Thread.currentThread().getName());  
  16.         for(int i=0; i<50; i++){  
  17.             if(i == 10){  
  18.                 //直接通过创建类对象来调用start()方法  
  19.                 new TreadOfextends().start();  
  20.                 new TreadOfextends().start();  
  21.             }  
  22.         }  
  23.   
  24.     }  
  25.   
  26. }  


 

      2)实现Runnable接口

重写run()方法,创建Runnable实例作为Thread的target。

例如:

[java]  view plain copy
  1. public class ThreadOfRun implements Runnable {  
  2.   
  3.     private int i;  
  4.     //实现Runnable接口中的run()方法  
  5.         public void run() {  
  6.             for(i=0; i<50; i++) {  
  7.                 System.out.println(Thread.currentThread().getName() + " " + i);  
  8.                 //通过实现接口来实现多线程  就不能通过this关键字来获取当前进程  
  9.             }  
  10.         }  
  11.     public static void main(String[] args) {  
  12.         for(int i=0; i<50; i++) {  
  13.             System.out.println(Thread.currentThread().getName() + " " + i);  
  14.             if(i == 10) {  
  15.                ThreadOfRun tor = new ThreadOfRun();  
  16.                //此处需要通过Thread的构造方法来new线程  
  17.                new Thread(tor , "线程1").start();  
  18.                new Thread(tor , "线程2").start();  
  19.             }  
  20.         }  
  21.   
  22.     }  
  23.       
  24.   
  25. }  


          3)Java 5以后可以通过更强大的手段——实现Callable接口

使用FutureTask对象作为Thread的对象的target创建并启动新线程

[java]  view plain copy
  1. import java.util.concurrent.Callable;  
  2. import java.util.concurrent.FutureTask;  
  3.   
  4. public class ThreadOfCallble implements Callable<Integer> {  
  5.                                         //支持泛型  
  6.     public Integer call() throws Exception {  
  7.         int i;  
  8.         for(i=0; i<50; i++) {  
  9.             System.out.println(Thread.currentThread().getName() + " " + i);  
  10.         }  
  11.         return i;//有返回值  
  12.     }  
  13.     public static void main(String[] args) {  
  14.         //创建Callable对象  
  15.         ThreadOfCallble toc = new ThreadOfCallble();  
  16.         //通过FutureTask来包装Callable对象  
  17.         FutureTask<Integer> ft = new FutureTask<Integer>(toc);  
  18.         for(int i=0; i<50; i++) {  
  19.             if(i ==10) {  
  20.                 new Thread(ft , "NewThread").start();  
  21.             }  
  22.         }  
  23.         try {  
  24.             //得到新线程的返回值  
  25.             System.out.println("子线程的返回值 : " + ft.get());  
  26.         }catch(Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30. }  


三种方式的对比 : 后两种方法非常适合多个相同的线程来处理同一份资源的情况,可以将CPU,代码和数据分开,比较符合面向对象的思想,而且还可以继承其他类,所以一般采用后两种方法。

oin线程

当在某个程序执行中调用其他线程的join方法时,条用线程将被阻塞,直至被join线程执行完为止。

[java]  view plain copy
  1. <pre class="java" name="code">public class ThreadOfjoin extends Thread {  
  2.       
  3.     public ThreadOfjoin(String name) {  
  4.         super(name);  
  5.     }  
  6.       
  7.     public void run() {  
  8.         for(int i=0; i<50; i++) {  
  9.             System.out.println(getName());  
  10.         }  
  11.     }  
  12.     public static void main(String[] args) {  
  13.         new ThreadOfjoin("NewThread").start();  
  14.         for(int i=0; i<50; i++) {  
  15.             if(i == 10) {  
  16.                 ThreadOfjoin toj = new ThreadOfjoin("JoinedThread");  
  17.                 toj.start();  
  18.                 try {  
  19.                     toj.join();//主线程调用了toj的join方法,需要等toj执行完主线程才能执行  
  20.                 } catch (InterruptedException e) {  
  21.                     e.printStackTrace();  
  22.                 }     
  23.             }  
  24.             System.out.println(Thread.currentThread().getName());  
  25.               
  26.         }  
  27.   
  28.     }  
  29.   
  30. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值