Java线程池
注意区分不是数据库连接池…我真蠢
线程池的作用:
线程池作用就是限制系统中执行线程的数量。
根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排 队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了;否则进入等待队列。
为什么要用线程池:
1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。
2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)。
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是ExecutorService。
比较重要的几个类:
ExecutorService:真正的线程池接口。
ScheduledExecutorService:能和Timer/TimerTask类似,解决那些需要任务重复执行的问题。
ThreadPoolExecutor:ExecutorService的默认实现。
ScheduledThreadPoolExecutor:继承ThreadPoolExecutor的ScheduledExecutorService接口实现,周期性任务调度的类实现。
要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,很有可能配置的线程池不是较优的,因此在Executors类里面提供了一些静态工厂,生成一些常用的线程池。
Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待.
newScheduledThreadPool创建一个定长线程池,支持定时及周期性任务执行.
newSingleThreadExecutor创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行.
Java Object
Clone(深拷贝浅拷贝);equals/hashCode(),为什么要重写他们,HashMap的实现原理一定会问,具体怎么实现的呢?toString(): Returns a string representation of the object.
Java数类型对象的安全性能
ConcurrentMap&&HashMap有什么区别
数据库线程池
连接池就是创建和管理一个连接的缓冲池技术,这些连接随时会被需要他们的线程调用。在我们连接数据库的时候如果不使用数据连接池,每一次访问数据库都去创建一个连接,这样会很消耗时间和项目的性能。
Proxool是sourceforge下面的一个开源数据连接池,最大的特色便是Proxool提供了监视的功能,便于发现连接泄漏的情况。
Mysql的事务机制
事务是怎么解决幻读脏读的,Seriazable(肯定不是,性能太差了).四种事务隔离层级.
Redis
Redis的setkey的timeout机制你觉得应该怎么实现。Redis为什么能当成一个锁使用:单线程?
Collection
集合类描述一下,都包含什么东西。没有Map.HashSet的特性是什么(去重无序)
Java的锁有哪些
1.3 synchronized和volatile比较
简单的说就是synchronized的代码块是确保可见性和原子性的, volatile只能确保可见性 当且仅当下面条件全部满足时, 才能使用volatile
-对变量的写入操作不依赖于变量的当前值, (++i/i++这种肯定不行), 或者能确保只有单个线程在更新
-该变量不会与其他状态变量一起纳入不变性条件中
-访问变量时不需要加锁
juc中的锁分两种, 1. 可重入锁; 2. 读写锁. 两者都用到了一个通用组件 AbstractQueuedSynchronizer.
利用了一个int来表示状态, 内部基于FIFO队列及UnSafe的CAS原语作为操纵状态的数据结构, AQS以单个 int 类型的原子变量来表示其状态,定义了4个抽象方法(tryAcquire(int)、tryRelease(int)、tryAcquireShared(int)、tryReleaseShared(int),前两个方法用于独占/排他模式,后两个用于共享模式)留给子类实现,用于自定义同步器的行为以实现特定的功能。
Java创建一个新的对象的方式
new/class.forName/getClass().newInstance; 调用java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法. 调用对象的clone()方法。运用反序列化手段,调用java.io.ObjectInputStream对象的readObject()方法.
查看磁盘分区的命令:df -hl.打印Tomcat日志目录的尾部:tail -n
Mysql存储过程总结
Mysql一般反而不使用存储过程迁移那些事务管理。
一.创建存储过程
1.基本语法:
create procedure sp_name()
begin
………
end
2.参数传递
3.调用
call sp_name()
4.删除存储过程
drop procedure sp_name
(1)不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程