1) 继承Thread类
1 public class TestThread { 2 public static void main(String[] args) { 3 ThreadDemo threadDemo = new ThreadDemo(); 4 threadDemo.start(); 5 } 6 } 7 8 9 class ThreadDemo extends Thread{ 10 11 @Override 12 public void run() { 13 boolean flag = false; 14 for(int i = 3 ; i < 100 ; i ++) { 15 flag = false; 16 for(int j = 2; j <= Math.sqrt(i) ; j++) { 17 if(i % j == 0) { 18 flag = true; 19 break; 20 } 21 } 22 if(flag == false) { 23 System.out.print(i+" "); 24 } 25 } 26 } 27 28 29 }
2)实现Runnable接口
1 public class TestRunnable { 2 public static void main(String[] args) { 3 RunnableDemo runnableDemo = new RunnableDemo(); 4 new Thread(runnableDemo).start(); 5 } 6 } 7 8 class RunnableDemo implements Runnable{ 9 10 @Override 11 public void run() { 12 boolean flag = false; 13 for(int i = 3 ; i < 100 ; i ++) { 14 flag = false; 15 for(int j = 2; j <= Math.sqrt(i) ; j++) { 16 if(i % j == 0) { 17 flag = true; 18 break; 19 } 20 } 21 if(flag == false) { 22 System.out.print(i+" "); 23 } 24 } 25 } 26 27 }
3)实现Callable接口
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.concurrent.Callable; 4 import java.util.concurrent.ExecutionException; 5 import java.util.concurrent.Executors; 6 import java.util.concurrent.FutureTask; 7 8 public class TestCallable1 { 9 public static void main(String[] args) throws InterruptedException, ExecutionException { 10 CallableDemo callableDemo = new CallableDemo(); 11 FutureTask futureTask = new FutureTask<>(callableDemo); 12 new Thread(futureTask).start(); 13 List<Integer> lists = (List<Integer>)futureTask.get(); //获取返回值 14 for (Integer integer : lists) { 15 System.out.print(integer + " "); 16 } 17 } 18 } 19 20 class CallableDemo implements Callable<List<Integer>>{ 21 22 @Override 23 public List<Integer> call() throws Exception { 24 boolean flag = false; 25 List<Integer> lists = new ArrayList<>(); 26 for(int i = 3 ; i < 100 ; i ++) { 27 flag = false; 28 for(int j = 2; j <= Math.sqrt(i) ; j++) { 29 if(i % j == 0) { 30 flag = true; 31 break; 32 } 33 } 34 if(flag == false) { 35 lists.add(i); 36 } 37 } 38 return lists; 39 } 40 41 }
4) 线程池
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.concurrent.Callable; 4 import java.util.concurrent.ExecutionException; 5 import java.util.concurrent.ExecutorService; 6 import java.util.concurrent.Executors; 7 import java.util.concurrent.Future; 8 9 public class TestThreadPool { 10 public static void main(String[] args) throws InterruptedException, ExecutionException { 11 ExecutorService executorService = Executors.newFixedThreadPool(5); 12 List<Future<List<Integer>>> ints = new ArrayList<>(); 13 for(int i = 0 ; i < 5; i ++) { 14 Future<List<Integer>> future = executorService.submit(new Callable<List<Integer>>() { 15 @Override 16 public List<Integer> call() throws Exception { 17 boolean flag = false; 18 System.out.println(Thread.currentThread().getName()+" "); 19 List<Integer> lists = new ArrayList<>(); 20 for(int i = 3 ; i < 100 ; i ++) { 21 flag = false; 22 for(int j = 2; j <= Math.sqrt(i) ; j++) { 23 if(i % j == 0) { 24 flag = true; 25 break; 26 } 27 } 28 if(flag == false) { 29 lists.add(i); 30 } 31 } 32 return lists; 33 } 34 }); 35 ints.add(future); 36 } 37 38 for (Future<List<Integer>> future : ints) { 39 System.out.println(future.get()); 40 } 41 } 42 } 43 44 class ThreadPoolDemo { 45 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律