Preface:
Although the java API to create multi-threaded, said only two ways (There are two ways to create a new thread of execution), are inherited
Thread
class to create and implementRunnable
interfaces to create, we show both on the blog post, see , but later added two JDK5.0, namely to achieveCallable
the interface to create and use线程池
to create two ways to create and analyze its characteristics after the demonstration on.Implement
Runnable
interface to create multiple threadsCreating steps:
1. Create an implementation
Callable
class interface.2. Rewrite call () method, the code thread to be executed are put call method.
3. Create achieve
Callable
instance of an object interface class.4. Step 3 of the object as a parameter to
FutureTask
the constructor, createsFutureTask
objects.5. The
FutureTask
object as a parameter toThread
the class, and calls to create an object start () method.
package day02;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
//创建一个多线程,输出20以内的偶数,并返回所有偶数的和
//1.创建一个实现`Callable`接口的类。
class TestSum implements Callable{
//2.重写call()方法,线程需要执行的代码都放到call方法中。
@Override
public Object call() throws Exception{
int sum = 0;
for(int i = 1;i <= 20 ;i++ ){
if(i % 2 == 0){
System.out.println(i);
sum = sum + i;
}
}
return sum;
}
}
public class ThreadCall {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//3.创建实现`Callable`接口类的实例对象。
TestSum test = new TestSum();
//4.将步骤 3 的对象作为参数传给`FutureTask`构造器中,创建`FutureTask`对象。
FutureTask futuretask = new FutureTask(test);
//5.将`FutureTask`的对象作为参数传给`Thread`类,创建对象并调用start()方法。
Thread thread = new Thread(futuretask);
thread.start();
//get方法可以获取返回值
System.out.println("偶数总合是:"+futuretask.get());
}
}
//输出结果:
2
4
6
8
10
12
14
16
18
20
偶数总合是:110
Callable interface to create multiple threads to achieve characteristics:
1.call () method may return a value, you can get () method to get the return value.
2.call () method can throw an exception, and can be captured outside.
3.Callable support generics.
Create multi-threaded using a thread pool
. Creating a way to achieve Runnable interface:
package day02;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Number implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}
public class ThreadPool {
public static void main(String[] args){
ExecutorService service = Executors.newFixedThreadPool(10);
Number num = new Number();
service.execute(num);
service.shutdown();
Second, create a way to achieve Callable interface:
package day02;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Number implements Callable {
@Override
public Object call() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
return null;
}
}
public class ThreadPool {
public static void main(String[] args){
ExecutorService service = Executors.newFixedThreadPool(10);
Number num = new Number();
service.submit(num);//区别在这里
service.shutdown();
}
}
Thread pool Benefits:
1. Frequent create threads and destroyed a large amount of resources, such as concurrent threads, a greater impact on performance, so you need to create a thread pool thread store, when using direct access, to achieve reuse and improve efficiency.
2. Reduce the thread creation time, improve the response speed.
3. Reduce the consumption of resources.
4. facilitate thread management.