NO IMAGE

實現多執行緒有以下四種方式:

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 < 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  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 < 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  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<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;
}
}
執行結果:
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<Future<List<Integer>>> ints = new ArrayList<>();
for(int i = 0 ; i < 5; i   ) {
Future<List<Integer>> future = executorService.submit(new Callable<List<Integer>>() {
@Override
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());
}
}
}
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種方式。