Documentation
¶
Index ¶
- func ThreadInterrupted() bool
- type AbortPolicy
- type CallerRunsPolicy
- type DefaultThreadFactory
- type DiscardOldestPolicy
- type DiscardPolicy
- type Executor
- type ExecutorService
- type RejectedExecutionHandler
- type ScheduledExecutorService
- type ScheduledFuture
- type ScheduledThreadPoolExecutor
- func (e *ScheduledThreadPoolExecutor) Schedule(command func(), delay time.Duration) ScheduledFuture
- func (e *ScheduledThreadPoolExecutor) ScheduleAtFixedRate(command func(), initialDelay time.Duration, period time.Duration) ScheduledFuture
- func (e *ScheduledThreadPoolExecutor) ScheduleWithFixedDelay(command func(), initialDelay time.Duration, delay time.Duration) ScheduledFuture
- type ThreadFactory
- type ThreadPool
- type ThreadPoolExecutor
- func NewCachedThreadPool() *ThreadPoolExecutor
- func NewFixedThreadPool(nThreads int) *ThreadPoolExecutor
- func NewSingleThreadExecutor() *ThreadPoolExecutor
- func NewThreadPoolExecutor(corePoolSize, maximumPoolSize int, keepAliveTime time.Duration, ...) *ThreadPoolExecutor
- func NewThreadPoolExecutorBasic(corePoolSize, maximumPoolSize int, keepAliveTime time.Duration, ...) *ThreadPoolExecutor
- func (e *ThreadPoolExecutor) AwaitTermination(timeout time.Duration) bool
- func (e *ThreadPoolExecutor) Execute(task func())
- func (e *ThreadPoolExecutor) GetCorePoolSize() int32
- func (e *ThreadPoolExecutor) GetPoolSize() int32
- func (e *ThreadPoolExecutor) GetQueue() util.BlockingQueue[func()]
- func (e *ThreadPoolExecutor) IsShutdown() bool
- func (e *ThreadPoolExecutor) IsTerminated() bool
- func (e *ThreadPoolExecutor) Shutdown()
- func (e *ThreadPoolExecutor) ShutdownNow() []func()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ThreadInterrupted ¶
func ThreadInterrupted() bool
Types ¶
type AbortPolicy ¶
type AbortPolicy struct{}
AbortPolicy 处理程序,抛出运行时异常(panic)。
func (*AbortPolicy) RejectedExecution ¶
func (p *AbortPolicy) RejectedExecution(r func(), executor ThreadPool)
type CallerRunsPolicy ¶
type CallerRunsPolicy struct{}
CallerRunsPolicy 处理程序,直接在调用者的线程中运行被拒绝的任务。
func (*CallerRunsPolicy) RejectedExecution ¶
func (p *CallerRunsPolicy) RejectedExecution(r func(), executor ThreadPool)
type DefaultThreadFactory ¶
type DefaultThreadFactory struct{}
DefaultThreadFactory 默认的线程工厂
func (*DefaultThreadFactory) NewThread ¶
func (f *DefaultThreadFactory) NewThread(r func())
type DiscardOldestPolicy ¶
type DiscardOldestPolicy struct{}
DiscardOldestPolicy 处理程序,丢弃最早的未处理请求,然后重试 execute。
func (*DiscardOldestPolicy) RejectedExecution ¶
func (p *DiscardOldestPolicy) RejectedExecution(r func(), executor ThreadPool)
type DiscardPolicy ¶
type DiscardPolicy struct{}
DiscardPolicy 处理程序,默默地丢弃被拒绝的任务。
func (*DiscardPolicy) RejectedExecution ¶
func (p *DiscardPolicy) RejectedExecution(r func(), executor ThreadPool)
type Executor ¶
type Executor interface {
// Execute 在将来的某个时间执行给定的命令。
// 该命令可以在新线程、池化线程或调用线程中执行,具体取决于 Executor 的实现。
Execute(task func())
}
Executor 执行器基础接口 对标 java.util.concurrent.Executor
type ExecutorService ¶
type ExecutorService interface {
Executor
// Shutdown 启动有序关闭,其中以前提交的任务将被执行,但不会接受新任务。
Shutdown()
// ShutdownNow 尝试停止所有正在执行的任务,暂停处理正在等待的任务,并返回等待执行的任务列表。
ShutdownNow() []func()
// IsShutdown 如果此执行程序已关闭,则返回 true。
IsShutdown() bool
// IsTerminated 如果关闭后所有任务都已完成,则返回 true。
IsTerminated() bool
// AwaitTermination 阻塞直到所有任务在关闭请求后完成,或发生超时,或当前线程被中断(以先发生者为准)。
AwaitTermination(timeout time.Duration) bool
}
ExecutorService 服务生命周期管理接口 对标 java.util.concurrent.ExecutorService
type RejectedExecutionHandler ¶
type RejectedExecutionHandler interface {
// RejectedExecution 当 Execute 不能接受某个任务时调用此方法。
// 为了解耦,这里传入 ThreadPool 接口而不是具体实现类。
RejectedExecution(r func(), executor ThreadPool)
}
RejectedExecutionHandler 无法由 ThreadPoolExecutor 执行的任务的处理程序。 对标 java.util.concurrent.RejectedExecutionHandler
type ScheduledExecutorService ¶
type ScheduledExecutorService interface {
ExecutorService
// Schedule 创建并执行在给定延迟后启用的一次性操作。
Schedule(command func(), delay time.Duration) ScheduledFuture
// ScheduleAtFixedRate 创建并执行一个在给定初始延迟后首次启用的定期操作,
// 后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,
// 然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
ScheduleAtFixedRate(command func(), initialDelay time.Duration, period time.Duration) ScheduledFuture
// ScheduleWithFixedDelay 创建并执行一个在给定初始延迟后首次启用的定期操作,
// 随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
ScheduleWithFixedDelay(command func(), initialDelay time.Duration, delay time.Duration) ScheduledFuture
}
ScheduledExecutorService 对标 java.util.concurrent.ScheduledExecutorService
type ScheduledFuture ¶
type ScheduledFuture interface {
// GetDelay 返回剩余的延迟时间
GetDelay() time.Duration
// Cancel 取消任务
Cancel(mayInterruptIfRunning bool) bool
// IsCancelled 任务是否已取消
IsCancelled() bool
// IsDone 任务是否已完成
IsDone() bool
}
ScheduledFuture 表示一个延迟的、结果导向的操作。 对标 java.util.concurrent.ScheduledFuture
type ScheduledThreadPoolExecutor ¶
type ScheduledThreadPoolExecutor struct {
*ThreadPoolExecutor
// contains filtered or unexported fields
}
func NewScheduledThreadPoolExecutor ¶
func NewScheduledThreadPoolExecutor(corePoolSize int) *ScheduledThreadPoolExecutor
func (*ScheduledThreadPoolExecutor) Schedule ¶
func (e *ScheduledThreadPoolExecutor) Schedule(command func(), delay time.Duration) ScheduledFuture
func (*ScheduledThreadPoolExecutor) ScheduleAtFixedRate ¶
func (e *ScheduledThreadPoolExecutor) ScheduleAtFixedRate(command func(), initialDelay time.Duration, period time.Duration) ScheduledFuture
func (*ScheduledThreadPoolExecutor) ScheduleWithFixedDelay ¶
func (e *ScheduledThreadPoolExecutor) ScheduleWithFixedDelay(command func(), initialDelay time.Duration, delay time.Duration) ScheduledFuture
type ThreadFactory ¶
type ThreadFactory interface {
NewThread(runnable func())
}
ThreadFactory 线程工厂接口 对标 java.util.concurrent.ThreadFactory
type ThreadPool ¶
type ThreadPool interface {
ExecutorService
// GetQueue 获取任务队列,DiscardOldestPolicy 需要用到它
GetQueue() util.BlockingQueue[func()]
}
ThreadPool 线程池特定接口,暴露了拒绝策略所需的内部状态(如队列)
type ThreadPoolExecutor ¶
type ThreadPoolExecutor struct {
// contains filtered or unexported fields
}
ThreadPoolExecutor v5: Physical Limit + Devirtualization + Unfair Scheduling
func NewCachedThreadPool ¶
func NewCachedThreadPool() *ThreadPoolExecutor
NewCachedThreadPool 对标 Executors.newCachedThreadPool
func NewFixedThreadPool ¶
func NewFixedThreadPool(nThreads int) *ThreadPoolExecutor
NewFixedThreadPool 对标 Executors.newFixedThreadPool
func NewSingleThreadExecutor ¶
func NewSingleThreadExecutor() *ThreadPoolExecutor
NewSingleThreadExecutor 对标 Executors.newSingleThreadExecutor
func NewThreadPoolExecutor ¶
func NewThreadPoolExecutor( corePoolSize, maximumPoolSize int, keepAliveTime time.Duration, workQueue util.BlockingQueue[func()], threadFactory ThreadFactory, handler RejectedExecutionHandler, ) *ThreadPoolExecutor
NewThreadPoolExecutor 全参数构造函数
func NewThreadPoolExecutorBasic ¶
func NewThreadPoolExecutorBasic( corePoolSize, maximumPoolSize int, keepAliveTime time.Duration, workQueue util.BlockingQueue[func()], ) *ThreadPoolExecutor
func (*ThreadPoolExecutor) AwaitTermination ¶
func (e *ThreadPoolExecutor) AwaitTermination(timeout time.Duration) bool
func (*ThreadPoolExecutor) Execute ¶
func (e *ThreadPoolExecutor) Execute(task func())
func (*ThreadPoolExecutor) GetCorePoolSize ¶
func (e *ThreadPoolExecutor) GetCorePoolSize() int32
func (*ThreadPoolExecutor) GetPoolSize ¶
func (e *ThreadPoolExecutor) GetPoolSize() int32
func (*ThreadPoolExecutor) GetQueue ¶
func (e *ThreadPoolExecutor) GetQueue() util.BlockingQueue[func()]
func (*ThreadPoolExecutor) IsShutdown ¶
func (e *ThreadPoolExecutor) IsShutdown() bool
func (*ThreadPoolExecutor) IsTerminated ¶
func (e *ThreadPoolExecutor) IsTerminated() bool
func (*ThreadPoolExecutor) Shutdown ¶
func (e *ThreadPoolExecutor) Shutdown()
func (*ThreadPoolExecutor) ShutdownNow ¶
func (e *ThreadPoolExecutor) ShutdownNow() []func()