【发布时间】:2018-05-29 14:32:22
【问题描述】:
其实,除了扩展Thread类和实现Runnable接口,还有什么其他的方法呢?
【问题讨论】:
-
扩展 Thread 类不是一个好办法!总是使用后者
-
基本上就是这样。还有其他方法可以执行 Runnables,如 ThreadPoolExecutor 等,但它们都需要实现
Runnable接口。
标签: java multithreading
其实,除了扩展Thread类和实现Runnable接口,还有什么其他的方法呢?
【问题讨论】:
Runnable 接口。
标签: java multithreading
只有一种方法可以在 Java 中创建新线程,那就是实例化 java.lang.Thread(要真正运行该线程,您还需要调用 start())。
在 Java 代码中创建线程的所有其他内容都回退到这种隐藏的方式(例如,ThreadFactory 实现将在某个时候实例化 Thread 对象,...)。
有两种不同的方式指定要在该线程中运行的代码:
java.lang.Runnable并将实现它的类的实例传递给the Thread constructor。Thread 本身并覆盖其run() 方法。第一种方法(实现Runnable)通常被认为是更正确的方法,因为您通常不会创建新的“种类”线程,而只是想在一个专用线程。
【讨论】:
Callable 与Runnable 非常相似,因为它本身不做任何与线程相关的事情,但它可以以各种方式用于传递代码以在某些情况下执行方式,包括在另一个线程中。因此,如果您将Callable 传递给您的ExecutorService,那么它很可能最终会在新线程上运行,但线程的实际实例化仍将通过new Thread 在幕后某处完成。
主要可以通过 3 种不同的方式创建线程
class SampleThread extends Thread {
//method where the thread execution will start
public void run(){
//logic to execute in a thread
}
//let’s see how to start the threads
public static void main(String[] args){
Thread t1 = new SampleThread();
Thread t2 = new SampleThread();
t1.start(); //start the first thread. This calls the run() method.
t2.start(); //this starts the 2nd thread. This calls the run() method.
}
}
class A implements Runnable{
@Override
public void run() {
// implement run method here
}
public static void main() {
final A obj = new A();
Thread t1 = new Thread(new A());
t1.start();
}
}
class Counter implements Callable {
private static final int THREAD_POOL_SIZE = 2;
// method where the thread execution takes place
public String call() {
return Thread.currentThread().getName() + " executing ...";
}
public static void main(String[] args) throws InterruptedException,
ExecutionException {
// create a pool of 2 threads
ExecutorService executor = Executors
.newFixedThreadPool(THREAD_POOL_SIZE);
Future future1 = executor.submit(new Counter());
Future future2 = executor.submit(new Counter());
System.out.println(Thread.currentThread().getName() + " executing ...");
//asynchronously get from the worker threads
System.out.println(future1.get());
System.out.println(future2.get());
}
}
支持线程池的 Executor 框架的 Callable 接口。
Runnable 或 Callable 接口优于扩展 Thread 类
【讨论】:
在java中创建线程实际上总共有4种方法:
java.lang.Thread 类java.lang.Runnable接口Callable接口。【讨论】:
在 Java 6 中启动线程的首选方式是使用 Executors:
ExecutorService es = Executors.newCachedThreadPool();
Runnable r = <your runnable here>;
es.execute(r);
【讨论】:
Thread,那不是办法。
execute(Runnable) 和另一个submit(Callable<T>)。
您已经提到了创建线程的方法只有两种,但还有第三种方法可以调用线程。
在 java1.5 中还有另一种调用线程的方法。那是通过“ExecutorService”。所有这些类都来自“java.util.concurrent”包。有多种方法可以使用“Executors”工厂类创建“ExecutorService”。以下是创建“ExecutorService”的一种方式..
ExecutorService es= Executors.newSingleThreadExecutor();
RunnableImpl r = new RunnableImpl();
未来 fu=es.submit(r);
使用“ExecutorService”方法,我们可以将八个 Runnable 或 Callable 提交给服务执行。
这不能说是创建线程的新方法。这是因为 ExecutorService 内部使用“ThreadFactory”类来创建一个新线程,该线程内部使用了第一种或第二种方法。所以不得不说,创建线程只有两种方式,但是java1.5中有一种新方式是调用线程而不是创建线程。
【讨论】:
在java中创建线程只有一种方法
即。线程类 start() 方法,但有不同的方式来运行线程 通过使用不同的方式
喜欢 1.Thread
2.可运行
3.RunnableFeature
4.可调用
5.ERexecutorService...等
【讨论】: