当前位置: 首页 > wzjs >正文

企业网站怎么做外链网站收录提交入口大全

企业网站怎么做外链,网站收录提交入口大全,深圳福田高端网站建设,软件商城免费下载app在实际开发中,我们经常遇到这样的场景。 需要立即初始化但计算成本高昂的值 val expensiveValue calculateExpensiveValue() 可能引发阻塞的初始化操作 val resource loadResourceFromNetwork()这些场景通常需要满足以下需求: 异步加载&#xff1a…

在实际开发中,我们经常遇到这样的场景。

  • 需要立即初始化但计算成本高昂的值
val expensiveValue = calculateExpensiveValue() 
  • 可能引发阻塞的初始化操作
val resource = loadResourceFromNetwork()

这些场景通常需要满足以下需求:

  1. 异步加载:避免阻塞主线流程
  2. 惰性计算:按需获取结果
  3. 结果缓存:多次访问时直接返回
  4. 状态同步:支持阻塞等待和异步等待
  5. 动态更新:允许主动设置最终值

异步计算值

通过 Kotlin 协程的 async/await 模型实现异步计算能力。AsyncValue 在初始化时立即通过 CoroutineScope.async 启动后台计算任务,并通过 Deferred.await() 实现挂起等待:

var finished = false
var result: T? = null
var exception: Throwable? = null
private var production: Deferred<T?> = CoroutineScope(dispatcher).async {runCatching { producer() }.onFailure { exception = it }.onSuccess { result = result ?: it }.getOrNull()
}

关键实现细节:

  1. 协程调度:使用 Dispatchers.IO 作为默认调度器,适用于 IO 密集型任务
  2. 原子操作:通过 runCatching 统一处理异常,确保结果状态一致性
  3. 结果缓存:计算完成后将结果存储在 result 字段中,后续访问直接返回
  4. 异常传递:异常信息会保留在 exception 字段中,等待消费时抛出

委托方式返回和设置值

通过实现 getValue/setValue 操作符函数,提供属性委托访问能力:

operator fun getValue(thisRef: Any?, property: KProperty<*>): T {val result = runBlocking { await() }if (result == null && exception != null) throw exception!!if (result == null) throw NullPointerException("The value[$property] produced is null")return result
}/**
* 获取值,如果值还未生产完成,则挂起协程等待
*/
suspend fun await(): T? = result ?: production.await().apply {if (exception != null) throw exception!!
}

特性说明:

  1. 阻塞等待:在普通上下文访问时,通过 runBlocking 阻塞当前线程等待结果
  2. 异常传播:保留原始异常堆栈信息,便于调试定位
  3. 空值防护:禁止返回 null 值,强制显式处理空值情况

动态更新能力:

operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {runCatching { production.cancel() }result = value
}

更新流程:

  1. 取消正在进行的异步计算
  2. 直接设置最终值
  3. 后续访问将返回新值

使用示例:

class ImageLoader {var currentImage: BufferedImage by AsyncValue {ImageIO.read(File("big_image.png")) // 耗时加载}// 动态切换图片fun changeImage(newImage: BufferedImage) {currentImage = newImage}// 允许产生空值val optional: Optional<String> by AsyncValue {Optional.ofNullable(null)}
}

完整代码

class AsyncValue<T>(dispatcher: CoroutineDispatcher = Dispatchers.IO,private val producer: suspend () -> T,
) {/*** 非委托下可以访问* 是否已经生产完成*/var finished = falseprivate set/*** 非委托下可以访问* 产生的值,如果值还未生产完成、异常报错、产生Null,则返回null*/var result: T? = nullprivate set/*** 非委托下可以访问* 产生的异常,如果异常未产生、未异常报错则返回null*/var exception: Throwable? = nullprivate setprivate var production: Deferred<T?> = CoroutineScope(dispatcher).async {runCatching { producer() }.onFailure { exception = it }.onSuccess { result = result ?: it }.getOrNull().apply { finished = true }}/*** 获取值,如果值还未生产完成,则挂起协程等待*/operator fun getValue(thisRef: Any?, property: KProperty<*>): T {val result = runBlocking { await() }if (result == null && exception != null) throw exception!!if (result == null) throw NullPointerException("The value[$property] produced is null")return result}operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {runCatching { production.cancel() }result = value}/*** 获取值,如果值还未生产完成,则挂起协程等待*/suspend fun await(): T? = result ?: production.await().apply {if (exception != null) throw exception!!}
}

典型应用场景

资源预加载

val font by AsyncValue {// 耗时字体加载Font.createFont(Font.TRUETYPE_FONT, File("big_font.ttf"))
}

网络请求聚合

class WeatherService {var weatherData by AsyncValue {apiService.fetchWeather() // 网络请求}fun showWeather() {// 页面显示时自动触发异步加载val data = weatherData updateUI(data)}
}

计算缓存优化

object DataProcessor {private val processedData by AsyncValue {heavyComputation().also { saveToCache(it) // 自动缓存计算结果}}fun getData() = processedData
}
http://www.dtcms.com/wzjs/383215.html

相关文章:

  • 网站开发流程 图书产品推广方案范文500字
  • 农村创业的好项目旺道seo
  • 做非法网站怎么规避软文推送
  • 滕州哪里有做网站的全网营销推广平台有哪些
  • 拼团手机网站开发aso优化推广公司
  • 三维网站是怎么做的哈尔滨关键词排名工具
  • 宜昌网站开发市场监督管理局投诉电话
  • 蕲春做网站网络营销平台排名
  • 建设网站所需技术东营优化公司
  • 怎样在华为云服务器里建设网站大搜推广
  • 网站备案审核通过后seo外包
  • 百度网站建设微信封面qq群引流推广平台
  • 中欣卡网上购物商城企业网站优化服务
  • 金融理财网站开发推广优化
  • 免费服装网站模板百度号码认证平台
  • 竞价推广哪里开户广西seo公司
  • 东北三省最新疫情通报宝鸡网站seo
  • 欧美网站模版前端性能优化有哪些方法
  • 网站导航的建设教育培训网页设计
  • 音乐网站建设流程项目推广方案
  • 网站建设主管6个好用的bt种子搜索引擎
  • 政府网站建设管理自查报告百度大数据分析平台
  • 仿99健康网网站源码深圳网络提速优化服务包
  • 购物网站为什么做移动端贵州网站seo
  • 真正免费手机网站建设免费发布信息平台有哪些
  • 哪里可以免费制作小程序南昌网优化seo公司
  • 郑州做个人网站的公司无锡百度公司王东
  • 梁山网站建设磁力天堂最新版地址
  • 太原网站建设baidu百度关键词seo排名优化
  • 城市建设网站金seo推广公司教程