cpu密集型
package PoolTime;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @BelongsProject: JAVAtest
* @BelongsPackage: PoolTime
* @Author:
* @CreateTime: 2023-02-26 19:30
* @Description: TODO
* @Version: 1.0
*/
public class CPUMainClass {
public static void main(String[] args) {
int availableProcessors = Runtime.getRuntime().availableProcessors();
System.out.println(availableProcessors);
//实例化一个固定大小为10个线程的newFixedThreadPool线程池
System.out.println("请输入线程池线程数,输入0则结束程序");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
while(num!=0){
long start = System.currentTimeMillis();
final CountDownLatch latch = new CountDownLatch(50);
ExecutorService excutorService = Executors.newFixedThreadPool(num);
// List<Long> list1 = new ArrayList<>();
// List<Long> list2 = new ArrayList<>();
for (int i = 0; i <50 ; i++) {
// excutorService.submit(new CPUTypeTest(list1,list2,latch));
excutorService.submit(new CPUTypeTest(latch));
}
try{
//使调用该方法的主线程处于等待状态,当倒数到0时主线程才执行。
latch.await();
long end = System.currentTimeMillis();
// System.out.println(start);
// System.out.println(end);
System.out.println(num+"个线程花费时间:" + (end - start));
} catch (InterruptedException e) {
throw new RuntimeException("处理异常",e);
}
//关闭线程池
excutorService.shutdown();
System.out.println("请输入线程池线程数");
num = scanner.nextInt();
}
scanner.close();
// excutorService.submit(new IOTypeTest(vector1,vector2));
}}
package PoolTime;
import java.util.concurrent.CountDownLatch;
/**
* @BelongsProject: JAVAtest
* @BelongsPackage: PoolTime
* @Author:
* @CreateTime: 2023-02-26 19:25
* @Description: TODO
* @Version: 1.0
*/
public class CPUTypeTest implements Runnable {
//
// // 整体执行时间,包括在队列中等待的时间
// List<Long> wholeTimeList;
// // 真正执行时间
// List<Long> runTimeList;
private long initStartTime = 0;
CountDownLatch latch;
/**
* 构造函数
* @param runTimeList
* @param wholeTimeList
*/
public CPUTypeTest(CountDownLatch latch) {
initStartTime = System.currentTimeMillis();
// this.runTimeList = runTimeList;
// this.wholeTimeList = wholeTimeList;
this.latch=latch;
}
/**
* 判断素数
* @param number
* @return
*/
public boolean isPrime(final int number) {
if (number <= 1)
return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0)
return false;
}
return true;
}
/**
* 計算素数
* @param
* @return
*/
public int countPrimes(final int lower, final int upper) {
int total = 0;
for (int i = lower; i <= upper; i++) {
if (isPrime(i))
total++;
}
return total;
}
public void run() {
long start = System.currentTimeMillis();
countPrimes(1, 1000000);
long end = System.currentTimeMillis();
this.latch.countDown();
// System.out.println("计数器"+this.latch);
// long wholeTime = end - initStartTime;
// long runTime = end - start;
// wholeTimeList.add(wholeTime);
// runTimeList.add(runTime);
// System.out.println(" 单个线程花费时间:" + (end - start));
}
}
IO密集型
package PoolTime.origin;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @BelongsProject: JAVAtest
* @BelongsPackage: PoolTime.origin
* @Author:
* @CreateTime: 2023-02-27 07:56
* @Description: TODO
* @Version: 1.0
*/
public class newThread {
public static void test(){
int availableProcessors = Runtime.getRuntime().availableProcessors();
System.out.println(availableProcessors);
//实例化一个固定大小为10个线程的newFixedThreadPool线程池
System.out.println("请输入线程池线程数,输入0则结束程序");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
while(num!=0){
long startTime=System.currentTimeMillis();
//获取开始时间
//实例化一个固定大小为10个线程的newFixedThreadPool线程池
ExecutorService excutorService = Executors.newFixedThreadPool(num);
final CountDownLatch latch = new CountDownLatch(150);
for (int i = 0; i <150; i++) {
int j = 1;
System.out.println(i == j);
//线程提交任务
excutorService.submit(new CalaulationStudentsData(i,latch));
}
try{
//使调用该方法的主线程处于等待状态,当倒数到0时主线程才执行。
latch.await();
long endTime=System.currentTimeMillis(); //获取结束时
System.out.println(num+"程序运行时间: "+(endTime-startTime)+"ms");
} catch (InterruptedException e) {
throw new RuntimeException("学生进入学校多线程处理异常",e);
}
//关闭线程池
excutorService.shutdown();
System.out.println("请输入线程池线程数");
num = scanner.nextInt();
}
scanner.close();
}
public static void main(String[] args) {
test();
}
}
package PoolTime.origin;
import java.io.*;
import java.util.concurrent.CountDownLatch;
/**
* @BelongsProject: JAVAtest
* @BelongsPackage: PoolTime.origin
* @Author: GuoYuan.Zhao
* @CreateTime: 2023-02-27 07:55
* @Description: TODO
* @Version: 1.0
*/
public class CalaulationStudentsData implements Runnable {
//学生数量
int studentNumber;
//锁存器
CountDownLatch latch;
/**
* @Author:
* @Description:
* @CreateTime: 2022/12/30 9:38
* @param: [studentNumber, latch]
* @return:
**/
public CalaulationStudentsData(int studentNumber, CountDownLatch latch){
this.studentNumber=studentNumber;
this.latch=latch;
}
/**
* @Author:Guoyuan.Zhao
* @Description:执行线程,这个是从接口实现的方法,线程去干什么
* @CreateTime: 2022/12/30 9:39
* @param: []
* @return: void
**/
@Override
public void run() {
try {
//计算学生学习数据的方法
this.CalculationStudentData();
//计数器减
latch.countDown();
}catch (Exception e){
throw new RuntimeException("学生进入学校执行数据异常"+e);
}
}
/**
* @Author:Guoyuan.Zhao
* @Description:计算学生数据的方法
* @CreateTime: 2022/12/30 9:40
* @param: []
* @return: void
**/
private void CalculationStudentData() throws InterruptedException, IOException {
//这里使用线程休眠的方式进行统计,因为频繁的io操作不会占用cpu资源,如有疑问,请参考下面链接
Thread.sleep(100);
}}
磁盘IO-为什么说IO密集型很少消耗CPU资源?
执行效果对比:
2*8 = 16
8+1 = 9
最终实验效果成功