描述:使用qt自带线程池方法处理任务,提高资源利用率。
1,修改继承的父类,将线程放到线程池中,去处理到来的任务。
界面:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void starting(int num);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "mythread.h"
#include <QThreadPool>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//创建子线程对象
GetRandNum *getRand = new GetRandNum;
BubbelSort *bubble = new BubbelSort;
QuickSort *quick = new QuickSort;
connect(this,&Widget::starting,getRand,&GetRandNum::reciveNum);
//启动子线程
connect(ui->start,&QPushButton::clicked,this,[=](){
emit starting(10000);
QThreadPool::globalInstance()->start(getRand);
});
connect(getRand,&GetRandNum::sendArray,bubble,&BubbelSort::recvArray);
connect(getRand,&GetRandNum::sendArray,quick,&QuickSort::recvArray);
connect(getRand, &GetRandNum::sendArray, this, [=](QVector<int> nums){
QThreadPool::globalInstance()->start(bubble);
QThreadPool::globalInstance()->start(quick);
for(int i=0; i<nums.size(); ++i)
{
ui->randList_2->addItem(QString::number((nums.at(i))));
}
});
connect(bubble, &BubbelSort::finish, this, [=](QVector<int> nums){
for(int i=0; i<nums.size(); ++i)
{
ui->bubbelList_2->addItem(QString::number((nums.at(i))));
}
});
connect(quick, &QuickSort::finish, this, [=](QVector<int> nums){
for(int i=0; i<nums.size(); ++i)
{
ui->quickList->addItem(QString::number((nums.at(i))));
}
});
}
Widget::~Widget()
{
delete ui;
}
线程:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QRunnable>
#include <QObject>
//1生成随机数的线程
class GetRandNum : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit GetRandNum(QObject *parent = nullptr);
void reciveNum(int num);
void run() override;
signals:
void sendArray(QVector<int> nums);
private:
int m_num;
};
//2生成处理冒泡排序的线程
class BubbelSort : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit BubbelSort(QObject *parent = nullptr);
void recvArray(QVector<int> array);
void bubbleSort(QVector<int> &nums);
void run() override;
signals:
void finish(QVector<int> nums);
private:
QVector<int> m_array;
};
//3生成处理快速排序的线程
class QuickSort : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit QuickSort(QObject *parent = nullptr);
void recvArray(QVector<int> array);
void quickSort(QVector<int> &nums,int l,int r);
void run() override;
signals:
void finish(QVector<int> nums);
private:
QVector<int> m_array;
};
#endif // MYTHREAD_H
#include "mythread.h"
#include <QDebug>
#include <QElapsedTimer>
#include <QVector>
#include <QThread>
GetRandNum::GetRandNum(QObject *parent)
: QObject(parent),QRunnable()
{
setAutoDelete(true);//程序结束自动销毁
}
//给全局变量赋值
void GetRandNum::reciveNum(int num)
{
m_num = num;
}
void GetRandNum::run()
{
qDebug() << "生成随机数的线程的线程地址: " << QThread::currentThread();
QVector<int> nums;
QElapsedTimer time;
time.start();
for(int i = 0;i<m_num;++i)
{
nums.push_back(qrand()%100000);
}
int useTime = time.elapsed();
qDebug() << "生成" << m_num << "个随机数总共用时:" << useTime << "毫秒";
emit sendArray(nums);
}
BubbelSort::BubbelSort(QObject *parent)
: QObject(parent),QRunnable()
{
}
void BubbelSort::recvArray(QVector<int> array)
{
m_array = array;
}
void BubbelSort::bubbleSort(QVector<int> &nums)
{
int n = nums.size();
bool isChange = false;
for(int i = 1;i<n;i++){
for(int j = 0;j<n-i-1;j++){
if(nums[j]>nums[j+1])
{
int t = nums[j];
nums[j] = nums[j+1];
nums[j+1] = t;
isChange = true;
}
}
if(!isChange){//冒泡排序优化了也比不上快排
break;
}
}
}
void BubbelSort::run()
{
qDebug() << "生成处理冒泡排序的线程地址: " << QThread::currentThread();
QElapsedTimer time;
time.start();
bubbleSort(m_array);
int useTime = time.elapsed();
qDebug() << "冒泡总共用时:" << useTime << "毫秒";
emit finish(m_array);
}
QuickSort::QuickSort(QObject *parent)
: QObject(parent),QRunnable()
{
}
void QuickSort::recvArray(QVector<int> array)
{
m_array = array;
}
void QuickSort::quickSort(QVector<int> &nums,int l,int r)
{
if (l < r)
{
int i = l, j = r;
// 拿出第一个元素, 保存到x中,第一个位置成为一个坑
int x = nums[l];
while (i < j)
{
// 从右向左找小于x的数
while (i < j && nums[j] >= x)
{
//左移, 直到遇到小于等于x的数
j--;
}
if (i < j)
{
//将右侧找到的小于x的元素放入左侧坑中, 右侧出现一个坑
//左侧元素索引后移
nums[i++] = nums[j];
}
// 从左向右找大于等于x的数
while (i < j && nums[i] < x)
{
//右移, 直到遇到大于x的数
i++;
}
if (i < j)
{
//将左侧找到的元素放入右侧坑中, 左侧出现一个坑
//右侧元素索引向前移动
nums[j--] = nums[i];
}
}
//此时 i=j,将保存在x中的数填入坑中
nums[i] = x;
quickSort(nums, l, i - 1); // 递归调用
quickSort(nums, i + 1, r);
}
}
void QuickSort::run()
{
qDebug() << "生成处理快速排序的线程地址: " << QThread::currentThread();
QElapsedTimer time;
time.start();
quickSort(m_array,0,m_array.size()-1);
int useTime = time.elapsed();
qDebug() << "快速排序总共用时:" << useTime << "毫秒";
emit finish(m_array);
}
执行: