java多线程实现的四种方式是什么_java创建多线程有哪些方式

java多线程实现的四种方式是什么_java创建多线程有哪些方式实现多线程有以下四种方式 实现多线程有以下四种方式:1. 继承Thread类 2.实现Runnable接口 3.实现Callable接口 4.线程池:提供了一个线程队列,队列中保存着所有等待状态的线程。避免了创建与销毁额外开销,提高了响应的速度。 体系结构: java.util.concurrent

java多线程实现的四种方式是什么_java创建多线程有哪些方式"

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

实现多线程有以下四种方式:
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种方式。

今天的文章java多线程实现的四种方式是什么_java创建多线程有哪些方式分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/48335.html

(0)
编程小号编程小号
上一篇 2023-09-04
下一篇 2023-09-04

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注