1、高并发产生的背景与价值
更高效的使用CPU,提高CPU利用率。没有绝对定义说系统是高并发能力的强弱,只是相对的概念,指单位时间内所能处理的请求数,只有相对比较时才能说谁的高并发能力高。
2、衡量高并发能力的常用指标
rt(响应时间) qps(每秒请求查询数) tps(每秒事务处理数)
1. 响应时间(RT) :响应时间是指系统对请求作出响应的时间
2、(QPS,Queries-per-second): 一般指每秒查询率。
业界参考
原理:每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间。
公式:( 总PV数 * 80% ) / ( 每天秒数 * 20% ) = 峰值时间每秒请求数(QPS) 。
机器:峰值时间每秒QPS / 单台机器的QPS = 需要的机器 。
每天300w PV 的在单台机器上,这台机器需要多少QPS?
( 3000000 * 0.8 ) / (86400 * 0.2 ) = 139 (QPS)。
一般需要达到139QPS,因为是峰值。
3、Java中对高并发处理:线程的实现方式
下面通过简单案例演示下:
package com.gary.concurrentprogramming.demo;
import java.util.ArrayList;
import java.util.concurrent.*;
/**
* 线程的实现方式
*/
public class ThreadDemo {
public static void main(String[] args) throws Exception {
// 1、线程重写run
MyThread thread = new MyThread();
thread.start();
// 2、无返回值任务
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Runnable 执行...");
}
}).start();
// 3、有返回值
Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("执行Callable...");
return "返回值:Callable";
}
};
String s = callable.call();
System.out.println(s);
// 4、线程池处理
ExecutorService exec = Executors.newCachedThreadPool();
Future<String> future = exec.submit(callable);
String returnFuture = future.get();
System.out.println(returnFuture);
System.out.println("==============================");
// 5、异步处理
FutureTask<String> futureTask = new FutureTask<String>(callable);
new Thread(futureTask).start();
String asyncCall = futureTask.get();
System.out.println(asyncCall);
// 6、行多个Callable任务,有多个返回值时,我们可以创建一个Future的集合
ExecutorService execW = Executors.newCachedThreadPool();
ArrayList<Future<String>> results = new ArrayList<Future<String>>();
for (int i = 0; i < 3; i++) {
results.add(execW.submit(new MyCallableTask(i)));
}
System.out.println("==============================");
for (Future<String> fs : results) {
System.out.println("fs="+fs.get());
if (fs.isDone()) {
try {
System.out.println("fs="+fs.get());
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("MyCallableTask任务未完成!");
}
}
execW.shutdown();
System.out.println("-----------7、------------------------>");
// 7、CompletableFuture
// 创建异步执行任务,有返回值
CompletableFuture<Double> cf = CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis());
return 1.2;
});
System.out.println("main thread start,time->"+System.currentTimeMillis());
//等待子任务执行完成
System.out.println("run result->"+cf.get());
System.out.println("main thread exit,time->"+System.currentTimeMillis());
}
public static class MyThread extends Thread {
@Override
public void run() {
System.out.println("MyThread 执行..." + Thread.currentThread().getName());
}
}
static class MyCallableTask implements Callable<String> {
private int id;
public MyCallableTask(int id){
this.id = id;
}
@Override
public String call() throws Exception {
for(int i = 0;i<2;i++){
System.out.println("Thread"+ id);
}
return "Result of callable: "+id;
}
}
}
最终的运行效果:
总结:
高并发的设计初衷是为了提高cpu利用率,从多道程序设计的多进程处理 到 分时系统 再到现在的多线程处理,通过这些技术提高cpu利用率,从而提高系统的高并发处理能力。而高并发只是一个相对的概念,不是绝对的定义。