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

【uniapp】uniapp使用java线程池

标题

由于js是性能孱弱的单线程语言,只要在渲染中执行了一些其他操作,会中断渲染,导致页面卡死,卡顿,吐司不消失等问题。在安卓端可以调用java线程池,把耗时操作写入线程池里面,优化性能。

实现

使用native.js,直接贴出代码

 class JavaExecutorPool {constructor() {// #ifdef APP-PLUSconst ScheduledThreadPoolExecutor = plus.android.importClass("java.util.concurrent.ScheduledThreadPoolExecutor")this.executor = new ScheduledThreadPoolExecutor(8)this.reusableRunnable = this.createReusableRunnable();this.block = null;// #endif}createReusableRunnable() {// #ifdef APP-PLUSlet that = this;return plus.android.implements("java.lang.Runnable", {// 用于存储动态逻辑run: function () {if (that.block) {console.log("JavaExecutorPool 执行动态逻辑")that.block(); // 执行动态逻辑that.block = null; // 执行完后清除逻辑}}});// #endif}execute(block) {// #ifdef APP-PLUSconsole.log("JavaExecutorPool 执行逻辑")this.block = block; // 动态注入逻辑this.executor.execute(this.reusableRunnable);// #endif}schedule(block, delay) {// #ifdef APP-PLUSif (delay <= 0){this.execute(block)return}setTimeout(() => {this.execute(block)}, delay)// #endif}shutdown() {// #ifdef APP-PLUSthis.executor.shutdown()// #endif}
}
export default JavaExecutorPool;
const javaExecutorPool = new JavaExecutorPool()

使用示例

// 在其他文件中
import javaExecutorPool from './JavaExecutorPool';javaExecutorPool.execute(() => {console.log("复用单例实例");
});
http://www.lryc.cn/news/528120.html

相关文章:

  • 面试题-Java集合框架
  • Java基础教程(007):方法的重载与方法的练习
  • 【ESP32】ESP-IDF开发 | WiFi开发 | TCP传输控制协议 + TCP服务器和客户端例程
  • npm cnpm pnpm npx yarn的区别
  • debian12.9编译freeswitch1.10.12【默认安装】
  • 使用 C/C++ 调用 libcurl 调试消息
  • 【愚公系列】《循序渐进Vue.js 3.x前端开发实践》030-自定义组件的插槽Mixin
  • 大一计算机的自学总结:异或运算
  • 通过protoc工具生成proto的pb.go文件以及使用protoc-go-inject-tag工具注入自定义标签
  • C语言练习(29)
  • Android实训九 数据存储和访问
  • 实验一---典型环节及其阶跃响应---自动控制原理实验课
  • SOME/IP--协议英文原文讲解2
  • matlab中,fill命令用法
  • 【Linux】Linux C判断两个IPv6地址是否有包含关系
  • 【玩转全栈】----Django基本配置和介绍
  • mysql 学习6 DML语句,对数据库中的表进行 增 删 改 操作
  • 自动化运维在云环境中的完整实践指南
  • 一分钟搭建promehteus+grafana+alertmanager监控平台
  • 【10.2】队列-设计循环队列
  • 设置jmeter界面图标字体大小
  • Xposed-Hook
  • 设计模式Python版 原型模式
  • QT:图像上绘制图形
  • GPU上没程序在跑但是显存被占用
  • wordpress代码结构解析
  • 【Unity3D】实现2D小地图效果
  • 关联传播和 Python 和 Scikit-learn 实现
  • https数字签名手动验签
  • LeetCode:62.不同路径