1、共享锁、排它锁、读写锁
- 共享锁:可以允许多个线程访问代码
- 排它锁:只有一个线程可以访问代码
- 读写锁:
- 读锁:读的时候,不允许写,但是允许同时读
- 写锁:写的时候,不允许写,也不允许读
2、统一锁、分段锁
- 统一锁:大粒度的锁
- 锁定A等待B;锁定B等待A->死锁 解决方案:把A+B统一成为大锁
- 分段锁:分成一段一段的小粒度的锁(ConcurrentHashMap)
3、间隙锁
就是两个值之间的间隙。为了解决幻读问题,InnoDB 只好引入新的锁,也就是 间隙锁 (Gap Lock)。幻读仅专指“新插入的行”。
4、行锁
给某一行加的锁
5、线程池的7个参数
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
/**
* Thread factory capturing access control context and class loader
*/
static class PrivilegedThreadFactory extends DefaultThreadFactory {
private final AccessControlContext acc;
private final ClassLoader ccl;
PrivilegedThreadFactory() {
super();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Calls to getContextClassLoader from this class
// never trigger a security check, but we check
// whether our callers have this permission anyways.
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
// Fail fast
sm.checkPermission(new RuntimePermission("setContextClassLoader"));
}
this.acc = AccessController.getContext();
this.ccl = Thread.currentThread().getContextClassLoader();
}
public Thread newThread(final Runnable r) {
return super.newThread(new Runnable() {
public void run() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
Thread.currentThread().setContextClassLoader(ccl);
r.run();
return null;
}
}, acc);
}
});
}
}
package com.itheima.security.springboot.test;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 自己的线程工具类
*
* @Auther: Vector
* @Date: ${DATE} * @Description: ${PACKAGE_NAME} * @version: 1.0
*/
public class ThreadPoolUtil {
/**
* 核心线程数
*/
private static final int SIZE_CORE_POOL = 5;
/**
* 最大线程数
*/
private static final int SIZE_MAX_POOL = 10;
/**
* 存活时间
*/
private static final long ALIVE_TIME = 2000;
/**
* 存放任务的队列
*/
private static BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<>(60);
/**
* The default rejected execution handler
*/
private static final RejectedExecutionHandler defaultHandler =
new ThreadPoolExecutor.AbortPolicy();
private static ThreadPoolExecutor pool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, ALIVE_TIME, TimeUnit.MILLISECONDS, bqueue, ThreadPoolUtil.myDefaultThreadFactory(), defaultHandler);
static {
pool.prestartAllCoreThreads();
}
public static ThreadPoolExecutor getPool() {
return pool;
}
public static ThreadFactory myDefaultThreadFactory() {
return new MyDefaultThreadFactory();
}
static class MyDefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
MyDefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "dcss-account" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
}