1.线程池可以创建线程统一的管理线程(统一创建、释放线程)
2.使用线程池方法实现点击开始按钮生成10000个随机数,然后分别使用冒泡排序和快速排序排序这10000个随机数,最后在窗口显示排序后的数字:
mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();
signals:void starting(int num);
private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mythread.h文件:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QRunnable>
//生成随机数
class MyThread : public QObject,public QRunnable //要使用Qt的信号槽继承就必须要继承QObject类(实现多继承)(注意QObject类要写在前面)
{Q_OBJECT
public:explicit MyThread(QObject *parent = nullptr);void recvNum(int num);void run() override;
signals:void sendArray(QVector<int> list);
private:int m_num;
};class BubbleSort : public QObject,public QRunnable
{Q_OBJECT
public:explicit BubbleSort(QObject *parent = nullptr);void recvArray(QVector<int> list);void run() override;
signals:void finish(QVector<int> num);
private:QVector<int> m_list;
};class QuickSort : public QObject,public QRunnable
{Q_OBJECT
public:explicit QuickSort(QObject *parent = nullptr);void recvArray(QVector<int> list);
private:void quickSort(QVector<int> &list, int l, int r);void run() override;
signals:void finish(QVector<int> num);
private:QVector<int> m_list;};
#endif // MYTHREAD_H
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);qRegisterMetaType<QVector<int>>("QVector<int>");MainWindow w;w.show();return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
#include <QThreadPool>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//1.创建任务类对象MyThread *gen = new MyThread;BubbleSort *bubble = new BubbleSort;QuickSort *quick = new QuickSort;connect(this, &MainWindow::starting, gen, &MyThread::recvNum);//2.启动子线程connect(ui->start, &QPushButton::clicked, this, [=]{emit starting(10000); //在启动子线程时,将要生成的随机数的个数发送出去QThreadPool::globalInstance()->start(gen); //将任务类对象(生成随机数)丢到线程池中});//接收子线程发送的数据connect(gen, &MyThread::sendArray, bubble, &BubbleSort::recvArray);connect(gen, &MyThread::sendArray, quick, &QuickSort::recvArray);connect(gen, &MyThread::sendArray, this, [=](QVector<int> list){ //connect里面不支持传递QVector类型//需要使用qRegisterMetaType()进行注册QThreadPool::globalInstance()->start(bubble); //将任务类对象(冒泡排序)丢到线程池中QThreadPool::globalInstance()->start(quick); //将任务类对象(快速排序)丢到线程池中for(int i = 0; i < list.size(); i++){ui->randList->addItem(QString::number(list.at(i)));}});connect(bubble, &BubbleSort::finish, this, [=](QVector<int> list){ //connect里面不支持传递QVector类型//需要使用qRegisterMetaType()进行注册for(int i = 0; i < list.size(); i++){ui->bubbleList->addItem(QString::number(list.at(i)));}});connect(quick, &QuickSort::finish, this, [=](QVector<int> list){ //connect里面不支持传递QVector类型//需要使用qRegisterMetaType()进行注册for(int i = 0; i < list.size(); i++){ui->quickList->addItem(QString::number(list.at(i)));}});\
}MainWindow::~MainWindow()
{delete ui;
}
mythread.cpp文件:
#include "mythread.h"
#include <QVector>
#include <QElapsedTimer> //计算某个流程执行所使用的时间
#include <QDebug>
#include <QThread>MyThread::MyThread(QObject *parent): QObject(parent), QRunnable()
{setAutoDelete(true); //设置当前这个线程的对象放到线程池里后,在工作完毕后自动释放
}void MyThread::recvNum(int num)
{m_num = num;
}void MyThread::run()
{qDebug() << "生成随机数的线程地址" << QThread::currentThread(); //获取一个指针,这个指针指向当前线程对象的地址QVector<int> list;QElapsedTimer time;time.start();for(int i = 0; i < m_num; i++){list.push_back(qrand() % 100000);}int milsec = time.elapsed();qDebug() << "生成" << m_num << "个随机数总共用时:" << milsec << "毫秒";emit sendArray(list);
}BubbleSort::BubbleSort(QObject *parent): QObject(parent), QRunnable()
{setAutoDelete(true); //设置当前这个线程的对象放到线程池里后,在工作完毕后自动释放
}void BubbleSort::recvArray(QVector<int> list)
{m_list = list;
}void BubbleSort::run()
{qDebug() << "冒泡排序的线程地址" << QThread::currentThread(); //获取一个指针,这个指针指向当前线程对象的地址QElapsedTimer time;time.start();for(int i = 0; i < m_list.size() - 1 ;i++){for(int j = 0;j < m_list.size() - i - 1; j++){if(m_list[j] > m_list[j + 1]){int temp = m_list[j];m_list[j] = m_list[j + 1];m_list[j + 1] = temp;}}}int milsec = time.elapsed();qDebug() << "冒泡排序用时" << milsec << "毫秒";emit finish(m_list);
}QuickSort::QuickSort(QObject *parent): QObject(parent), QRunnable()
{setAutoDelete(true); //设置当前这个线程的对象放到线程池里后,在工作完毕后自动释放
}void QuickSort::recvArray(QVector<int> list)
{m_list = list;
}void QuickSort::quickSort(QVector<int> &s, int l, int r)
{if(l < r){int i = l, j = r;int x = s[l];while(i < j){while(i < j &&s[j] >=x){j--;}if(i < j){s[i++] = s[j];}while(i < j && s[i] < x){i++;}if(i < j){s[j--] = s[i];}}s[i] = x;quickSort(s, l, i - 1);quickSort(s, i + 1, r);}
}void QuickSort::run()
{qDebug() << "快速排序的线程地址" << QThread::currentThread(); //获取一个指针,这个指针指向当前线程对象的地址QElapsedTimer time;time.start();quickSort(m_list, 0,m_list.size()-1);int milsec = time.elapsed();qDebug() << "快速排序用时" << milsec << "毫秒";emit finish(m_list);
}
运行结果:
通过运行结果可以发现:生成随机数的线程和冒泡排序的线程是使用线程池中的同一个线程,生成随机数的线程结束后就空闲了,然后又来了两个任务冒泡排序和快速排序,所以就又使用了这个空闲的任务来运行冒泡排序,然后快速排序用到了线程池里面的另一个线程。通过这点可以知道:通过线程池可以最大程度利用线程,减少资源的浪费。