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 }
    复制代码
  • 相关阅读:
    [转]三五个人十来条枪 如何走出软件作坊成为开发正规军
    [转]asp.net页面字体变大问题总结
    [练手7]传值和传引用区别
    [练手3]选择排序
    [练手5]希尔排序
    oracle用select for update 中断后无法操作表的处理:杀掉SESSION
    [转]最省时管理法:让你一天随意
    [练手4]插入排序
    没有对“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files”的写访问权限
    NUnit单元测试编写指南
  • 原文地址:https://www.cnblogs.com/duguangming/p/11047448.html
Copyright © 2011-2022 走看看