首页 > 编程语言 > 详细

多线程实现的四种方式

时间:2020-06-04 16:11:10      阅读:44      评论:0      收藏:0      [点我收藏+]

实现多线程有以下四种方式:

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() : 创建固定大小的线程,可以延迟或定时的执行任务。
————————————————
版权声明:本文为CSDN博主「刘信坚的博客」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_38974634/java/article/details/81315900

1) 继承Thread类

package thread;

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 < 100 ; 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+" ");
}
}

}
}

2)实现Runnable接口

package thread;

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 < 100 ; 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)实现Callable接口

package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CallableDemo callableDemo = new CallableDemo();
FutureTask futureTask = new FutureTask<>(callableDemo);
new Thread(futureTask).start();
List<Integer> lists = (List<Integer>)futureTask.get(); //获取返回值
for (Integer integer : lists) {
System.out.print(integer + " ");
}
}
}

class CallableDemo implements Callable<List<Integer>>{

@Override
public List<Integer> call() throws Exception {
boolean flag = false;
List<Integer> lists = new ArrayList<>();
for(int i = 3 ; i < 100 ; 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;
}

}

4) 线程池

package thread;

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<Future<List<Integer>>> ints = new ArrayList<>();
for(int i = 0 ; i < 5; i ++) {
Future<List<Integer>> future = executorService.submit(new Callable<List<Integer>>() {
public List<Integer> call() throws Exception {
boolean flag = false;
System.out.println(Thread.currentThread().getName()+" ");
List<Integer> lists = new ArrayList<>();
for(int i = 3 ; i < 100 ; 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<List<Integer>> future : ints) {
System.out.println(future.get());
}
}
}

 

多线程实现的四种方式

原文:https://www.cnblogs.com/feiyu8888/p/13042099.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!