使用线程池和窗口池优化electron

概念

窗口池和线程池是两个不同的概念。

窗口池是指在Electron中同时创建多个窗口,并对这些窗口进行管理和维护的机制。窗口池可以帮助开发者更好地管理和控制应用中的窗口,从而提高应用的性能和稳定性。在窗口池中,可以对窗口进行创建、销毁、隐藏、显示等操作,以满足不同的应用场景和需求。

线程池是指在Electron主进程中使用Node.js提供的线程池模块worker_threads来实现多线程处理的机制。线程池可以将一些耗时的计算、IO等操作分配到多个线程中进行处理,从而不会阻塞主线程,保证应用的流畅性和稳定性。在线程池中,可以对线程进行创建、销毁、调度等操作,以满足不同的应用场景和需求。

实现

线程池的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { Worker } = require('worker_threads');
 
// 创建线程池
const workerPool = {
  size: 4, // 线程池大小
  workers: [],
  activeWorkers: 0,
  taskQueue: [],
  init() {
    for (let i = 0; i < this.size; i++) {
      const worker = new Worker('./worker.js');
      worker.on('message', this.handleMessage.bind(this));
      worker.on('error', this.handleError.bind(this));
      worker.on('exit', this.handleExit.bind(this));
      this.workers.push(worker);
    }
  },
  handleMessage(msg) {
    // 处理返回结果
    const { task, result } = msg;
    task.callback(result);
    this.activeWorkers--;
    this.schedule();
  },
  handleError(err) {
    // 处理错误信息
    console.error(err);
    this.activeWorkers--;
    this.schedule();
  },
  handleExit(worker) {
    // 处理退出线程
    console.log(`worker ${worker.threadId} exited`);
    this.activeWorkers--;
    this.schedule();
  },
  schedule() {
    // 调度任务
    while (this.taskQueue.length > 0 && this.activeWorkers < this.size) {
      const task = this.taskQueue.shift();
      this.dispatch(task);
    }
  },
  dispatch(task) {
    // 分配任务给空闲线程
    const worker = this.workers.shift();
    worker.postMessage(task);
    this.activeWorkers++;
    this.workers.push(worker);
  },
  addTask(task) {
    // 添加任务到任务队列
    this.taskQueue.push(task);
    this.schedule();
  },
};
 
// 创建任务
function taskFactory(data, callback) {
  return {data, callback, }
}
 
// 将任务添加到线程池
function runTask(data, callback) {
const task = taskFactory(data, callback)
 workerPool.addTask(task)
}
 
// 初始化线程池
workerPool.init()
 
// 在主线程中创建任务并提交到线程池处理
runTask({ num: 1000000 }, result => { console.log(result); });

 窗口池实现

复制代码
const { BrowserWindow } = require('electron');

class WindowPool {
  constructor(options) {
    this.options = options || {};
    this.pool = [];
    this.index = 0;
  }

  createWindow() {
    const win = new BrowserWindow(this.options);
    this.pool.push(win);
    return win;
  }

  destroyWindow(win) {
    const index = this.pool.indexOf(win);
    if (index !== -1) {
      this.pool.splice(index, 1);
      win.destroy();
    }
  }

  hideWindow(win) {
    if (win && !win.isDestroyed()) {
      win.hide();
    }
  }

  showWindow(win) {
    if (win && !win.isDestroyed()) {
      win.show();
    }
  }

  nextWindow() {
    if (this.pool.length === 0) {
      return null;
    }
    this.index = (this.index + 1) % this.pool.length;
    return this.pool[this.index];
  }
}
复制代码

 

posted @   养只猫叫土豆  阅读(1255)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示