Java多线程实现的四种方式_多线程实现的四种方式
实现多线程有以下四种方式:
1. 继承Thread类
2.实现Runnable接口
3.实现Callable接口
4.线程池:提供了一个线程队列,队列中保存着所有等待状态的线程。避免了创建与销毁额外开销,提高了响应的速度。
体系结构:
java.util.concurrent.Executor : 负责线程的使用与调度的根接口
|--ExecutorService 子接口: 线程池的主要接口
|--ThreadPoolExecutor 线程池的实现类
|--ScheduledExecutorService 子接口:负责线程的调度
|--ScheduledThreadPoolExecutor :继承 ThreadPoolExecutor, 实现 ScheduledExecutorService *
工具类 : Executors
ExecutorService newFixedThreadPool() : 创建固定大小的线程池
ExecutorService newCachedThreadPool() : 缓存线程池,线程池的数量不固定,可以根据需求自动的更改数量。
ExecutorService newSingleThreadExecutor() : 创建单个线程池。线程池中只有一个线程
ScheduledExecutorService newScheduledThreadPool() : 创建固定大小的线程,可以延迟或定时的执行任务。
1) 继承Thread类package com.lxj.juc;
public class TestThread {
public static void main(String[] args) {
ThreadDemo threadDemo = new ThreadDemo();
threadDemo.start();
}
}
class ThreadDemo extends Thread{
@Override
public void run() {
boolean flag = false;
for(int i = 3 ; i
flag = false;
for(int j = 2; j <= Math.sqrt(i) ; j++) {
if(i % j == 0) {
flag = true;
break;
}
}
if(flag == false) {
System.out.print(i+" ");
}
}
}
}
运行结果:
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
2)实现Runnable接口package com.lxj.juc;
public class TestRunnable {
public static void main(String[] args) {
RunnableDemo runnableDemo = new RunnableDemo();
new Thread(runnableDemo).start();
}
}
class RunnableDemo implements Runnable{
@Override
public void run() {
boolean flag = false;
for(int i = 3 ; i
flag = false;
for(int j = 2; j <= Math.sqrt(i) ; j++) {
if(i % j == 0) {
flag = true;
break;
}
}
if(flag == false) {
System.out.print(i+" ");
}
}
}
}
运行结果:
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
3)实现Callable接口package com.lxj.juc;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class TestCallable1 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CallableDemo callableDemo = new CallableDemo();
FutureTask futureTask = new FutureTask<>(callableDemo);
new Thread(futureTask).start();
List lists = (List)futureTask.get(); //获取返回值
for (Integer integer : lists) {
System.out.print(integer + " ");
}
}
}
class CallableDemo implements Callable>{
@Override
public List call() throws Exception {
boolean flag = false;
List lists = new ArrayList<>();
for(int i = 3 ; i
flag = false;
for(int j = 2; j <= Math.sqrt(i) ; j++) {
if(i % j == 0) {
flag = true;
break;
}
}
if(flag == false) {
lists.add(i);
}
}
return lists;
}
}
运行结果:
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
4) 线程池package com.lxj.juc;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestThreadPool {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(5);
List>> ints = new ArrayList<>();
for(int i = 0 ; i
Future> future = executorService.submit(new Callable>() {
@Override
public List call() throws Exception {
boolean flag = false;
System.out.println(Thread.currentThread().getName()+" ");
List lists = new ArrayList<>();
for(int i = 3 ; i
flag = false;
for(int j = 2; j <= Math.sqrt(i) ; j++) {
if(i % j == 0) {
flag = true;
break;
}
}
if(flag == false) {
lists.add(i);
}
}
return lists;
}
});
ints.add(future);
}
for (Future> future : ints) {
System.out.println(future.get());
}
}
}
class ThreadPoolDemo {
}
运行结果:
pool-1-thread-2
pool-1-thread-5
pool-1-thread-3
pool-1-thread-1
pool-1-thread-4
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
以上即为创建多线程的4种方式。