有关Thread

Thread

 

setDaemon 设置守护进程

线程分为守护线程 和用户线程;当所用用户线程结束时,守护线程也随之结束;需在线程启动前调用。

 

Thread.yield() 线程让步

让出CPU使用权 让线程达到就绪状态 与其他线程共同竞争CPU 优先级高的线程不一定一定先执行。做出让步的线程 亦有可能重新执行。

Thread.join() 等待当前线程执行完 再接下去执行 可用于线程按顺序执行

 

 

Thread.run() 只是调用方法 不新开启线程

Thread.start() 新增线程 让其达到就绪状态 等待执行run() 方法。

 

wait sleep 的区别

sleep属于Thread wait 属于Object

sleep 不会释放当前占有的锁 wait释放锁

wait() notify() 会对锁进行操作 只有synchronized 中进行

notify() 会随机唤醒一个睡眠线程 一般采用notifyAll();

wait() 方法一般放在:

while(!执行条件){

wait();

} notifyAll()的时候可以唤醒。

 

Runnable 用来实现多线程 Thread implement Runnable

public interface Runnable {

/**

* When an object implementing interface <code>Runnable</code> is used

* to create a thread, starting the thread causes the object's

* <code>run</code> method to be called in that separately executing

* thread.

* <p>

* The general contract of the method <code>run</code> is that it may

* take any action whatsoever.

*

* @see java.lang.Thread#run()

*/

public abstract void run();

}

Callable 带返回值 可抛出异常

public interface Callable<V> {

/**

* Computes a result, or throws an exception if unable to do so.

*

* @return computed result

* @throws Exception if unable to compute a result

*/

V call() throws Exception;

}

如何取到返回值[会阻塞主线程]:

1)通过FutureTask FutureTask 构造方法 传入callable 构造Thread 传入FutureTask FutureTask.get() 取到callable 返回

FutureTask implements RunnableFuture<V>

RunnableFuture<V> extends Runnable, Future<V>

2) ExecutorService sumbit() 获取Future get()。

Future 控制处理执行结果

public interface Future<V> 
{ 
boolean cancel(boolean mayInterruptIfRunning); 
boolean isCancelled(); boolean isDone();
 V get() throws InterruptedException, ExecutionException; 
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }

FutureTask Future的实现类

Thread.holdsLock(this) 当前线程持有了对象锁

 

volatile 和atomic 的不同

volatile 保证对所有线程可见性 但不保证原子性。多个线程读取到的数据都是最新的 但回写过程可能会导致异常。写操作一定在读操作之前.(可用于关闭线程)

atomic 通过CAS(compare and swap)保证原子性

CAS "我认为V的值应该为A,如果是,那么将V的值更新为B,否则不修改并告诉V的值实际为多少"

 

独占锁和乐观锁

独占锁:synchronized就是一种独占锁,它假设最坏的情况,并且只有在确保其它线程不会造成干扰的情况下执行,会导致其它所有需要锁的线程挂起,等待持有锁的线程释放锁

乐观锁:每次不加锁而是假设没有冲突而去完成某项操作,如果因为冲突失败就重试,直到成功为止

 

 

ThreadPoolExectuor 线程池

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue<Runnable> workQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler)

corePoolSize 核心线程数 默认情况下无初始化线程 ,除非设置了prestartCoreThread或prestartAllCoreThreads

maximumPoolSize 最大线程数

keepAliveTime 非核心线程存活时间 若设置了allowCoreThreadTimeOut(boolean) 则核心线程也会被终止。

unit keepAliveTime 的单位

workQueue 阻塞队列

threadFactory 线程工厂 创建线程

handler 超过maximumPoolSize 时候的策略

 

添加任务 当前运行的线程小于核心线程 则创建核心线程处理任务 。当线程数大于核心线程小于最大线程数 若队列不满 入队列,若队列满的状态下 会创建线程(非核心) 当队列已满且达到最大线程数 由RejectedExecutionHandler 抛出异常。

 

阻塞队列

SynchronousQueue 无容量 无缓冲的队列 每一个put操作必须等待一个take操作,否则不能继续添加元素。

采用队列TransferQueue来实现公平性策略,采用堆栈TransferStack来实现非公平性策略

链接:https://blog.csdn.net/qq_38293564/article/details/80604194

 

LinkedBlockingQueue 无界缓冲队列 生产和消费都有独立的锁去控制

 

https://www.cnblogs.com/java-zhao/p/5135958.html

 

ArrayBlockingQueue 有界 生产消费用同一个锁去控制

 

ReenterLock 重入锁

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值