1、企业级应用成熟到一定程度后,数据库的读写会成为系统瓶颈。现在互联网中的各种分库分表、主从分离 就是为了快速的响应客户请求。现在各种缓存肆虐,内存已经不再是限制,所以是否考虑丢弃mysql等关系型数据库了?NO,针对一定的统计数据及落地数据还是要的。
现在可以考虑让系统主线程读写缓存,直接跟内存打交道,不再沟通物理DB。
如下代码:
package
com.afengzi.jeast.thread;
import
java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors;
public
class
ThreadOptimize {
public
static
void
main(String[] args) {
long
start = System.currentTimeMillis() ;
readFromDatabase();
System.
out
.println(
"readFromDatabase time 0 : "
+(System.currentTimeMillis ()-start));
long
start2 = System.currentTimeMillis() ;
optimizeReadFromDatabase();
System.
out
.println(
"optimizeReadFromDatabase time 0 : "
+(System.currentTimeMillis ()-start2));
}
private
static
void
readFromDatabase(){
try
{
Thread. sleep(1000);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
private
static
void
optimizeReadFromDatabase(){
ExecutorService executer = Executors. newCachedThreadPool() ;
executer.execute(
new
ReadDatabaseThread());
}
public
static
class
ReadDatabaseThread
implements
Runnable{
@Override
public
void
run() {
readFromDatabase();
}
}
}
输出:
readFromDatabase time 0 : 1000
optimizeReadFromDatabase time 0 : 61