为什么callable还是没有实现多线程
答案:2 悬赏:50 手机版
解决时间 2021-02-19 20:07
- 提问者网友:姑娘长的好罪过
- 2021-02-19 01:03
为什么callable还是没有实现多线程
最佳答案
- 五星知识达人网友:不甚了了
- 2021-02-19 01:08
Runnable和Callable的区别是,
(1)Callable规定的方法是call(),Runnable规定的方法是run().
(2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值得
(3)call方法可以抛出异常,run方法不可以
(4)运行Callable任务可以拿到一个Future对象,表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。通过Future对象可以了解任务执行情况,可取消任务的执行,还可获取执行结果。
1、通过实现Runnable接口来创建Thread线程:
步骤1:创建实现Runnable接口的类:
class SomeRunnable implements Runnable
{
public void run()
{
//do something here
}
}
步骤2:创建一个类对象:
Runnable oneRunnable = new SomeRunnable();
步骤3:由Runnable创建一个Thread对象:
Thread oneThread = new Thread(oneRunnable);
步骤4:启动线程:
oneThread.start();
至此,一个线程就创建完成了。
注释:线程的执行流程很简单,当执行代码oneThread.start();时,就会执行oneRunnable对象中的void run();方法,
该方法执行完成后,线程就消亡了。
2、与方法1类似,通过实现Callable接口来创建Thread线程:其中,Callable接口(也只有一个方法)定义如下:
public interface Callable<V>
{
V call() throws Exception;
}
步骤1:创建实现Callable接口的类SomeCallable<Integer>(略);
步骤2:创建一个类对象:
Callable<Integer> oneCallable = new SomeCallable<Integer>();
步骤3:由Callable<Integer>创建一个FutureTask<Integer>对象:
FutureTask<Integer> oneTask = new FutureTask<Integer>(oneCallable);
注释:FutureTask<Integer>是一个包装器,它通过接受Callable<Integer>来创建,它同时实现了Future和Runnable接口。
步骤4:由FutureTask<Integer>创建一个Thread对象:
Thread oneThread = new Thread(oneTask);
步骤5:启动线程:
oneThread.start();
至此,一个线程就创建完成了。
3、通过继承Thread类来创建一个线程:
步骤1:定义一个继承Thread类的子类:
class SomeThead extends Thraad
{
public void run()
{
//do something here
}
}
步骤2:构造子类的一个对象:
SomeThread oneThread = new SomeThread();
步骤3:启动线程:
oneThread.start();
至此,一个线程就创建完成了。
注释:这种创建线程的方法不够好,主要是因为其涉及运行机制问题,影响程序性能。
4、通过线程池来创建线程:
步骤1:创建线程池:
ExecutorService pool = Executors.newCachedThreadPool();
步骤2:通过Runnable对象或Callable对象将任务提交给ExecutorService对象:
Future<Integer> submit(Callable<Integer> task);
注释:Future是一个接口,它的定义如下:
public interface Future<T>
{
V get() throws ...;
V get(long timeout, TimeUnit unit) throws ...;
void cancle(boolean mayInterrupt);
boolean isCancelled();
boolean isDone();
}
至此,一个线程就创建完成了。
注释:线程池需调用shutdown();方法来关闭线程。
5、通过事件分配线程直接使用程序中的原有线程:
使用方法:
直接调用EventQueue类的静态方法invokeLater():
EventQueue.invokeLater(oneRunnable);
注释:调用EventQueue.invokeLater(oneRunnable);会直接执行oneRunnable对象中的run()方法
(1)Callable规定的方法是call(),Runnable规定的方法是run().
(2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值得
(3)call方法可以抛出异常,run方法不可以
(4)运行Callable任务可以拿到一个Future对象,表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。通过Future对象可以了解任务执行情况,可取消任务的执行,还可获取执行结果。
1、通过实现Runnable接口来创建Thread线程:
步骤1:创建实现Runnable接口的类:
class SomeRunnable implements Runnable
{
public void run()
{
//do something here
}
}
步骤2:创建一个类对象:
Runnable oneRunnable = new SomeRunnable();
步骤3:由Runnable创建一个Thread对象:
Thread oneThread = new Thread(oneRunnable);
步骤4:启动线程:
oneThread.start();
至此,一个线程就创建完成了。
注释:线程的执行流程很简单,当执行代码oneThread.start();时,就会执行oneRunnable对象中的void run();方法,
该方法执行完成后,线程就消亡了。
2、与方法1类似,通过实现Callable接口来创建Thread线程:其中,Callable接口(也只有一个方法)定义如下:
public interface Callable<V>
{
V call() throws Exception;
}
步骤1:创建实现Callable接口的类SomeCallable<Integer>(略);
步骤2:创建一个类对象:
Callable<Integer> oneCallable = new SomeCallable<Integer>();
步骤3:由Callable<Integer>创建一个FutureTask<Integer>对象:
FutureTask<Integer> oneTask = new FutureTask<Integer>(oneCallable);
注释:FutureTask<Integer>是一个包装器,它通过接受Callable<Integer>来创建,它同时实现了Future和Runnable接口。
步骤4:由FutureTask<Integer>创建一个Thread对象:
Thread oneThread = new Thread(oneTask);
步骤5:启动线程:
oneThread.start();
至此,一个线程就创建完成了。
3、通过继承Thread类来创建一个线程:
步骤1:定义一个继承Thread类的子类:
class SomeThead extends Thraad
{
public void run()
{
//do something here
}
}
步骤2:构造子类的一个对象:
SomeThread oneThread = new SomeThread();
步骤3:启动线程:
oneThread.start();
至此,一个线程就创建完成了。
注释:这种创建线程的方法不够好,主要是因为其涉及运行机制问题,影响程序性能。
4、通过线程池来创建线程:
步骤1:创建线程池:
ExecutorService pool = Executors.newCachedThreadPool();
步骤2:通过Runnable对象或Callable对象将任务提交给ExecutorService对象:
Future<Integer> submit(Callable<Integer> task);
注释:Future是一个接口,它的定义如下:
public interface Future<T>
{
V get() throws ...;
V get(long timeout, TimeUnit unit) throws ...;
void cancle(boolean mayInterrupt);
boolean isCancelled();
boolean isDone();
}
至此,一个线程就创建完成了。
注释:线程池需调用shutdown();方法来关闭线程。
5、通过事件分配线程直接使用程序中的原有线程:
使用方法:
直接调用EventQueue类的静态方法invokeLater():
EventQueue.invokeLater(oneRunnable);
注释:调用EventQueue.invokeLater(oneRunnable);会直接执行oneRunnable对象中的run()方法
全部回答
- 1楼网友:老鼠爱大米
- 2021-02-19 01:33
public static void main(string[] args) { // todo auto-generated method stub thr hh=new thr(); //futuretask a=new futuretask(hh); thread t1=new thread(new futuretask(hh),"aaa"); t1.start();//开启线程aaa system.out.println(1); thread t2=new thread(new futuretask(hh),"bbb"); t2.start();//开启线程bbb system.out.println(2); thread t3=new thread(new futuretask(hh),"ccc"); t3.start();//开启线程ccc system.out.println(3); }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯