zoukankan      html  css  js  c++  java
  • 多线程实现的四种方法

    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 }
  • 相关阅读:
    Spring MVC 核心组件详解
    Spring MVC 入门就这一篇
    Spring 事务解决方案
    【UGUI源码分析】Unity遮罩之Mask详细解读
    游戏开发中不同时区下的时间问题
    ARTS第十三周(阅读Tomcat源码)
    Win10 电脑安装.NET低版本提示“这台计算机中已经安装了 .NET Framwork 4.6.2或版本更高的更新”问题
    Dynamics 365 Setup 提示SqlServer 存在
    Dynamics CRM "Verification of prerequisites for Domain Controller promotion failed. Certificate Server is installed."
    Dynamics CRM
  • 原文地址:https://www.cnblogs.com/yanpingping/p/11006740.html
Copyright © 2011-2022 走看看