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

Qt的QThread、QRunnable和QThreadPool的使用

1.相关描述

随机生产1000个数字,然后进行冒泡排序与快速排序。随机生成类继承QThread类、冒泡排序使用moveToThread方法添加到一个线程中、快速排序类继承QRunnable类,添加到线程池中进行排序。

 2.相关界面

3.相关代码

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "tgeneratenum.h"
#include "tbubblesort.h"
#include "tquicksort.h"
#include <QDebug>
#include <QThreadPool>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);qDebug() << "主线程:" << QThread::currentThread();/********************  多线程的使用方法一 ****************/TGenerateNum *gen = new TGenerateNum;    // 继承QThread类/******************* 多线程的使用方法二 **********************/QThread *bubbleThread = new QThread;TBubbleSort *bubble = new TBubbleSort;bubble->moveToThread(bubbleThread);/******************* 多线程的使用方法三 线程池的使用 **********************/TQuickSort *quicksort = new TQuickSort;QThreadPool::globalInstance()->setMaxThreadCount(10);// 按钮的信号槽connect(ui->btnStart, &QPushButton::clicked, this, [=](){gen->setCnt(10000);gen->start();});// 生成随机数connect(gen, &TGenerateNum::sendList, this, [=](QVector<int> list){quicksort->setList(list);QThreadPool::globalInstance()->start(quicksort);bubbleThread->start(); // 开启冒泡排序线程for(int i = 0; i < list.size(); i++){ui->listWidgetGenerate->addItem(QString::number(list[i]));}});connect(gen, &TGenerateNum::sendList, bubble, &TBubbleSort::working);connect(bubble, &TBubbleSort::sendList, this, [=](QVector<int> list){for(int i = 0; i < list.size(); i++){ui->listWidgetBubbleSort->addItem(QString::number(list[i]));}});connect(quicksort, &TQuickSort::sendList, this, [=](QVector<int> list){for(int i = 0; i < list.size(); i++){ui->listWidgetQuickSort->addItem(QString::number(list[i]));}});// 释放内存connect(this, &Widget::destroyed, this, [=](){gen->quit();gen->wait();gen->deleteLater();bubbleThread->quit();bubbleThread->wait();bubbleThread->deleteLater();bubble->deleteLater();});
}Widget::~Widget()
{delete ui;
}

生成随机数:

tgeneratenum.h

#ifndef TGENERATENUM_H
#define TGENERATENUM_H#include <QObject>
#include <QThread>class TGenerateNum : public QThread
{Q_OBJECT
public:TGenerateNum(QObject *parent=nullptr);void setCnt(qint32 cnt);// QThread interface
protected:void run() override;
signals:void sendList(QVector<int> list);private:qint32 m_cnt;
};
#endif // TGENERATENUM_H

tgeneratenum.cpp

#include "tgeneratenum.h"
#include <QRandomGenerator>
#include <QVector>
#include <QElapsedTimer>
#include <QDebug>TGenerateNum::TGenerateNum(QObject *parent):QThread(parent) {}void TGenerateNum::setCnt(qint32 cnt)
{m_cnt = cnt;
}void TGenerateNum::run()
{qDebug() << "生成随机数线程地址:" << QThread::currentThread();QElapsedTimer time;time.start();QVector<int> list;for(int i = 0; i < m_cnt; i++){int num = QRandomGenerator::global()->bounded(100000);list.push_back(num);}int milsec = time.elapsed();qDebug() << "生成" << m_cnt << "个随机数总共用时:" << milsec << "毫秒";emit sendList(list);
}

生成随机数,需要加时间种子

冒泡排序:

tbubblesort.h

#ifndef TBUBBLESORT_H
#define TBUBBLESORT_H#include <QObject>class TBubbleSort : public QObject
{Q_OBJECT
public:explicit TBubbleSort(QObject *parent = nullptr);void working(QVector<int> list);
signals:void sendList(QVector<int> list);
private:void bubbleSort(QVector<int>& list);QVector<int> m_list;
};#endif // TBUBBLESORT_H

tbubblesort.cpp

#include "tbubblesort.h"
#include <QElapsedTimer>
#include <QDebug>
#include <QThread>TBubbleSort::TBubbleSort(QObject *parent): QObject{parent}
{}void TBubbleSort::working(QVector<int> list)
{qDebug() << "冒泡线程地址:" << QThread::currentThread();QElapsedTimer time;time.start();this->bubbleSort(list);int milsec = time.elapsed();qDebug() << "冒泡排序总共用时:" << milsec << "毫秒";emit sendList(list);
}void TBubbleSort::bubbleSort(QVector<int> &list)
{for (int i = 0; i < list.size(); i++){for (int j = 1; j < list.size()-i; j++){if (list[j - 1] > list[j]){int temp = list[j - 1];list[j - 1] = list[j];list[j] = temp;}}}
}

快速排序:

tquicksort.h

#ifndef TQUICKSORT_H
#define TQUICKSORT_H#include <QObject>
#include <QRunnable>class TQuickSort : public QObject, public QRunnable
{Q_OBJECT
public:explicit TQuickSort(QObject *parent = nullptr);void setList(QVector<int> list);
signals:void sendList(QVector<int> list);// QRunnable interface
public:void run() override;
private:void quick_sort(QVector<int>& list, int l, int r);QVector<int> m_list;
};#endif // TQUICKSORT_H

tquicksort.cpp

#include "tquicksort.h"
#include <QElapsedTimer>
#include <QDebug>
#include <QThread>
TQuickSort::TQuickSort(QObject *parent): QObject{parent}, QRunnable()
{// 开启自动回收,由线程池回收对象资源setAutoDelete(true);
}void TQuickSort::setList(QVector<int> list)
{m_list = list;
}void TQuickSort::run()
{qDebug() << "快排线程地址:" << QThread::currentThread();QElapsedTimer time;time.start();this->quick_sort(m_list, 0, m_list.size());int milsec = time.elapsed();qDebug() << "快速排序总共用时:" << milsec << "毫秒";emit sendList(m_list);
}void TQuickSort::quick_sort(QVector<int>& list, int l, int r){//如果小于等于1个数据元素·直接返回结束快排函数 r为数组元素总个数if(l+1 >= r){return ;}int first = l, last = r-1, key = list[first];while(first < last){while(first < last && list[last] >= key){--last;}//如果值小于 key分界值 交换list[first] = list[last];while(first < last && list[first] < key){++first;}//如果值大于key分界值 交换list[last] = list[first];}list[first] = key;//递归左右部分进行快排quick_sort(list, l, first);quick_sort(list, first+1, r);
}

http://www.lryc.cn/news/307294.html

相关文章:

  • windows安装部署node.js并搭建Vue项目
  • 【计算机】本科考研还是就业?
  • ChatGPT调教指南 | 咒语指南 | Prompts提示词教程(三)
  • 小程序一键链接WIFI
  • 结构体位域保存传感器数据
  • 66-ES6:var,let,const,函数的声明方式,函数参数,剩余函数,延展操作符
  • prime_series_level-1靶场详解
  • LeetCode刷题笔记之二叉树(三)
  • IBM在闪存系统集成实时恶意软件I/O检测功能
  • bpmn-js中实现xml数据转为json数据
  • HUAWEI Programming Contest 2024(AtCoder Beginner Contest 342)(A,B,C,D,E,F,G)
  • 解决Docker镜像中CentOS 8仓库问题
  • 顶顶通呼叫中心中间件-如何使处于机器人话术中的通话手动转接到坐席分机上讲解(mod_cti基于FreeSWITCH)
  • HarmonyOS—使用数据模型和连接器
  • 基于MQTT协议实现微服务架构事件总线
  • 免费的Git图形界面工具sourceTree介绍
  • 【Appium UI自动化】pytest运行常见错误解决办法
  • IDEA如何开启Dashboard
  • 【论文复现】——一种新的鲁棒三维点云平面拟合方法
  • 【C语言】学生宿舍信息管理系统
  • 用Python插入页码到PDF文档
  • LabVIEW光偏振态转换及检测仿真系统
  • scp 本地机和远程机传输文件的方法
  • 自定义神经网络二之模型训练推理
  • Java设计模式:单例模式之六种实现方式详解(二)
  • 开创5G无线新应用:笙科电子5.8GHz 射频芯片
  • 使用 JMeter 生成测试数据对 MySQL 进行压力测试
  • C# cass10 面积计算
  • 中间件-Nginx漏洞整改(限制IP访问隐藏nginx版本信息)
  • Xcode与Swift开发小记