您的位置:首页 > 编程语言 > Java开发

Java多线程之Callable接口的实现

2013-09-23 14:34 357 查看
Java代码


importjava.util.concurrent.Callable;

importjava.util.concurrent.ExecutorService;

importjava.util.concurrent.Executors;

importjava.util.concurrent.Future;

/***//**

*Callable和Future接口

*Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。

*Callable和Runnable有几点不同:

*(1)Callable规定的方法是call(),而Runnable规定的方法是run().

*(2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。

*(3)call()方法可抛出异常,而run()方法是不能抛出异常的。

*(4)运行Callable任务可拿到一个Future对象,

*Future表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。

*通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。

*/

publicclassCallableAndFuture{

/***//**

*自定义一个任务类,实现Callable接口

*/

publicstaticclassMyCallableClassimplementsCallable{

//标志位

privateintflag=0;

publicMyCallableClass(intflag){

this.flag=flag;

}

publicStringcall()throwsException{

if(this.flag==0){

//如果flag的值为0,则立即返回

return"flag=0";

}

if(this.flag==1){

//如果flag的值为1,做一个无限循环

try{

while(true){

System.out.println("looping.");

Thread.sleep(2000);

}

}catch(InterruptedExceptione){

System.out.println("Interrupted");

}

return"false";

}else{

//falg不为0或者1,则抛出异常

thrownewException("Badflagvalue!");

}

}

}

publicstaticvoidmain(String[]args){

//定义3个Callable类型的任务

MyCallableClasstask1=newMyCallableClass(0);

MyCallableClasstask2=newMyCallableClass(1);

MyCallableClasstask3=newMyCallableClass(2);

//创建一个执行任务的服务

ExecutorServicees=Executors.newFixedThreadPool(3);

try{

//提交并执行任务,任务启动时返回了一个Future对象,

//如果想得到任务执行的结果或者是异常可对这个Future对象进行操作

Futurefuture1=es.submit(task1);

//获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行

System.out.println("task1:"+future1.get());

Futurefuture2=es.submit(task2);

//等待5秒后,再停止第二个任务。因为第二个任务进行的是无限循环

Thread.sleep(5000);

System.out.println("task2cancel:"+future2.cancel(true));

//获取第三个任务的输出,因为执行第三个任务会引起异常

//所以下面的语句将引起异常的抛出

Futurefuture3=es.submit(task3);

System.out.println("task3:"+future3.get());

}catch(Exceptione){

System.out.println(e.toString());

}

//停止任务执行服务

es.shutdownNow();

}

}

转载自:http://aloys.iteye.com/blog/630135

另外有关多线程简单介绍的文章

java中的多线程
002
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。
003
那么:为什么我们不能直接调用run()方法呢?
004
我的理解是:线程的运行需要本地操作系统的支持。
005
如果你查看start的源代码的时候,会发现:
006
注意我用红色加粗的那一条语句,说明此处调用的是start0()。并且这个这个方法用了native关键字,次关键字表示调用本地操作系统的函数。因为多线程的实现需要本地操作系统的支持。
007
class
helloextendsThread{
008
009
public
hello(){
010
011
}
012
013
public
hello(Stringname){
014
this.name
=name;
015
}
016
017
public
voidrun(){
018
for
(inti=0;i<5;i++){
019
System.out.println(name
+"运行"+i);
020
}
021
}
022
023
public
staticvoidmain(String[]args){
024
hello
h1=newhello("A");
025
hello
h2=newhello("B");
026
h1.start();
027
h2.start();
028
}
029
030
private
Stringname;
031
}
032
033
034
class
helloimplementsRunnable{
035
036
public
hello(){
037
038
}
039
040
public
hello(Stringname){
041
this.name
=name;
042
}
043
044
public
voidrun(){
045
for
(inti=0;i<5;i++){
046
System.out.println(name
+"运行"+i);
047
}
048
}
049
050
public
staticvoidmain(String[]args){
051
hello
h1=newhello("线程A");
052
Thread
demo=newThread(h1);
053
hello
h2=newhello("线程B");
054
Thread
demo1=newThread(h2);
055
demo.start();
056
demo1.start();
057
}
058
059
private
Stringname;
060
}
061
062
063
Thread和Runnable的区别:
064
如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。
065
class
MyThreadimplementsRunnable{
066
067
private
intticket=5;//5张票
068
069
public
voidrun(){
070
for
(inti=0;i<=20;i++){
071
if
(this.ticket>0){
072
System.out.println(Thread.currentThread().getName()+
"正在卖票"+this.ticket--);
073
}
074
}
075
}
076
}
077
public
classlzwCode{
078
079
public
staticvoidmain(String[]args){
080
MyThread
my=newMyThread();
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
092
1):适合多个相同的程序代码的线程去处理同一个资源
093
094
2):可以避免java中的单继承的限制
095
096
3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。
097
098
099
【使用线程同步解决问题】
100
101
采用同步的话,可以使用同步代码块和同步方法两种来完成。
102
103
class
helloimplementsRunnable{
104
public
voidrun(){
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
staticvoidmain(String[]args){
120
hello
he=newhello();
121
Thread
h1=newThread(he);
122
Thread
h2=newThread(he);
123
Thread
h3=newThread(he);
124
h1.start();
125
h2.start();
126
h3.start();
127
}
128
private
intcount=5;
129
}
130
131
也可以采用同步方法。
132
133
语法格式为synchronized
方法返回类型方法名(参数列表){
134
135
//
其他代码
136
137
}
138
139
class
helloimplementsRunnable{
140
public
voidrun(){
141
for
(inti=0;i<10;++i){
142
sale();
143
}
144
}
145
146
public
synchronizedvoidsale(){
147
if
(count>0){
148
try
{
149
Thread.sleep(1000);
150
}
catch(InterruptedExceptione){
151
e.printStackTrace();
152
}
153
System.out.println(count--);
154
}
155
}
156
157
public
staticvoidmain(String[]args){
158
hello
he=newhello();
159
Thread
h1=newThread(he);
160
Thread
h2=newThread(he);
161
Thread
h3=newThread(he);
162
h1.start();
163
h2.start();
164
h3.start();
165
}
166
167
private
intcount=5;
168
}
转载自:http://www.oschina.net/code/snippet_930709_24979
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 多线程