Java多线程之Callable接口的实现

来源:互联网 发布:淘宝打折软件怎么设置 编辑:程序博客网 时间:2024/05/02 01:48
Java代码  收藏代码
  1. import java.util.concurrent.Callable;     
  2. import java.util.concurrent.ExecutorService;     
  3. import java.util.concurrent.Executors;     
  4. import java.util.concurrent.Future;     
  5.     
  6. /** *//**   
  7.  * Callable 和 Future接口   
  8.  * Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。   
  9.  * Callable和Runnable有几点不同:   
  10.  * (1)Callable规定的方法是call(),而Runnable规定的方法是run().   
  11.  * (2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。   
  12.  * (3)call()方法可抛出异常,而run()方法是不能抛出异常的。   
  13.  * (4)运行Callable任务可拿到一个Future对象,   
  14.  * Future 表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。   
  15.  * 通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。   
  16.  */    
  17. public class CallableAndFuture {     
  18.     
  19.     /** *//**   
  20.      * 自定义一个任务类,实现Callable接口   
  21.      */    
  22.     public static class MyCallableClass implements Callable{     
  23.         // 标志位     
  24.         private int flag = 0;     
  25.         public MyCallableClass(int flag){     
  26.             this.flag = flag;     
  27.         }     
  28.         public String call() throws Exception{     
  29.             if (this.flag == 0){     
  30.                 // 如果flag的值为0,则立即返回     
  31.                 return "flag = 0";     
  32.             }      
  33.             if (this.flag == 1){     
  34.                 // 如果flag的值为1,做一个无限循环     
  35.                 try {     
  36.                     while (true) {     
  37.                         System.out.println("looping.");     
  38.                         Thread.sleep(2000);     
  39.                     }     
  40.                 } catch (InterruptedException e) {     
  41.                     System.out.println("Interrupted");     
  42.                 }     
  43.                 return "false";     
  44.             } else {     
  45.                 // falg不为0或者1,则抛出异常     
  46.                 throw new Exception("Bad flag value!");     
  47.             }     
  48.         }     
  49.     }     
  50.          
  51.     public static void main(String[] args) {     
  52.         // 定义3个Callable类型的任务     
  53.         MyCallableClass task1 = new MyCallableClass(0);     
  54.         MyCallableClass task2 = new MyCallableClass(1);     
  55.         MyCallableClass task3 = new MyCallableClass(2);     
  56.              
  57.         // 创建一个执行任务的服务     
  58.         ExecutorService es = Executors.newFixedThreadPool(3);     
  59.         try {     
  60.             // 提交并执行任务,任务启动时返回了一个 Future对象,     
  61.             // 如果想得到任务执行的结果或者是异常可对这个Future对象进行操作     
  62.             Future future1 = es.submit(task1);     
  63.             // 获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行     
  64.             System.out.println("task1: " + future1.get());     
  65.                  
  66.             Future future2 = es.submit(task2);     
  67.             // 等待5秒后,再停止第二个任务。因为第二个任务进行的是无限循环     
  68.             Thread.sleep(5000);     
  69.             System.out.println("task2 cancel: " + future2.cancel(true));     
  70.                  
  71.             // 获取第三个任务的输出,因为执行第三个任务会引起异常     
  72.             // 所以下面的语句将引起异常的抛出     
  73.             Future future3 = es.submit(task3);     
  74.             System.out.println("task3: " + future3.get());     
  75.         } catch (Exception e){     
  76.             System.out.println(e.toString());     
  77.         }     
  78.         // 停止任务执行服务     
  79.         es.shutdownNow();     
  80.     }     
  81. }
转载自:  http://aloys.iteye.com/blog/630135

另外有关多线程简单介绍的文章
java中的多线程
002在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。
003那么:为什么我们不能直接调用run()方法呢?
004我的理解是:线程的运行需要本地操作系统的支持。
005如果你查看start的源代码的时候,会发现:
006注意我用红色加粗的那一条语句,说明此处调用的是start0()。并且这个这个方法用了native关键字,次关键字表示调用本地操作系统的函数。因为多线程的实现需要本地操作系统的支持。
007class hello extends Thread {
008  
009    public hello() {
010  
011    }
012  
013    public hello(String name) {
014        this.name = name;
015    }
016  
017    public void run() {
018        for (int i = 0; i < 5; i++) {
019            System.out.println(name + "运行     " + i);
020        }
021    }
022  
023    public static void main(String[] args) {
024        hello h1=new hello("A");
025        hello h2=new hello("B");
026        h1.start();
027        h2.start();
028    }
029  
030    private String name;
031}
032 
033 
034class hello implements Runnable {
035  
036    public hello() {
037  
038    }
039  
040    public hello(String name) {
041        this.name = name;
042    }
043  
044    public void run() {
045        for (int i = 0; i < 5; i++) {
046            System.out.println(name + "运行     " + i);
047        }
048    }
049  
050    public static void main(String[] args) {
051        hello h1=new hello("线程A");
052        Thread demo= new Thread(h1);
053        hello h2=new hello("线程B");
054        Thread demo1=new Thread(h2);
055        demo.start();
056        demo1.start();
057    }
058  
059    private String name;
060}
061 
062 
063Thread和Runnable的区别:
064如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。
065class MyThread implements Runnable{
066  
067    private int ticket = 5;  //5张票
068  
069    public void run() {
070        for (int i=0; i<=20; i++) {
071            if (this.ticket > 0) {
072                System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--);
073            }
074        }
075    }
076}
077public class lzwCode {
078      
079    public static void main(String [] args) {
080        MyThread my = new MyThread();
081        new Thread(my, "1号窗口").start();
082        new Thread(my, "2号窗口").start();
083        new Thread(my, "3号窗口").start();
084    }
085}
086 
087 
088总结一下吧:
089 
090实现Runnable接口比继承Thread类所具有的优势:
091 
0921):适合多个相同的程序代码的线程去处理同一个资源
093 
0942):可以避免java中的单继承的限制
095 
0963):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。
097 
098 
099【使用线程同步解决问题】
100 
101采用同步的话,可以使用同步代码块和同步方法两种来完成。
102 
103class hello implements Runnable {
104    public void run() {
105        for(int i=0;i<10;++i){
106            synchronized (this) {
107                if(count>0){
108                    try{
109                        Thread.sleep(1000);
110                    }catch(InterruptedException e){
111                        e.printStackTrace();
112                    }
113                    System.out.println(count--);
114                }
115            }
116        }
117    }
118  
119    public static void main(String[] args) {
120        hello he=new hello();
121        Thread h1=new Thread(he);
122        Thread h2=new Thread(he);
123        Thread h3=new Thread(he);
124        h1.start();
125        h2.start();
126        h3.start();
127    }
128    private int count=5;
129}
130 
131也可以采用同步方法。
132 
133语法格式为synchronized 方法返回类型方法名(参数列表){
134 
135    // 其他代码
136 
137}
138 
139class hello implements Runnable {
140    public void run() {
141        for (int i = 0; i < 10; ++i) {
142            sale();
143        }
144    }
145  
146    public synchronized void sale() {
147        if (count > 0) {
148            try {
149                Thread.sleep(1000);
150            } catch (InterruptedException e) {
151                e.printStackTrace();
152            }
153            System.out.println(count--);
154        }
155    }
156  
157    public static void main(String[] args) {
158        hello he = new hello();
159        Thread h1 = new Thread(he);
160        Thread h2 = new Thread(he);
161        Thread h3 = new Thread(he);
162        h1.start();
163        h2.start();
164        h3.start();
165    }
166  
167    private int count = 5;
168}
转载自:http://www.oschina.net/code/snippet_930709_24979