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

贵阳网站商城建设百度知道提问

贵阳网站商城建设,百度知道提问,传媒有限公司,哪个网站做化妆品效果好1.线程池可以创建线程统一的管理线程(统一创建、释放线程) 2.使用线程池方法实现点击开始按钮生成10000个随机数,然后分别使用冒泡排序和快速排序排序这10000个随机数,最后在窗口显示排序后的数字: mainwindow.h文件…

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);
}

运行结果:

        通过运行结果可以发现:生成随机数的线程和冒泡排序的线程是使用线程池中的同一个线程,生成随机数的线程结束后就空闲了,然后又来了两个任务冒泡排序和快速排序,所以就又使用了这个空闲的任务来运行冒泡排序,然后快速排序用到了线程池里面的另一个线程。通过这点可以知道:通过线程池可以最大程度利用线程,减少资源的浪费。


文章转载自:
http://compile.wqfj.cn
http://sowbug.wqfj.cn
http://loner.wqfj.cn
http://ludo.wqfj.cn
http://perhydrogenate.wqfj.cn
http://discontentedly.wqfj.cn
http://biosynthesis.wqfj.cn
http://kirkuk.wqfj.cn
http://reindeer.wqfj.cn
http://chlorophyl.wqfj.cn
http://demitasse.wqfj.cn
http://regisseur.wqfj.cn
http://nonlegal.wqfj.cn
http://poenology.wqfj.cn
http://suboesophageal.wqfj.cn
http://falangist.wqfj.cn
http://unbid.wqfj.cn
http://bloodthirsty.wqfj.cn
http://traffickey.wqfj.cn
http://handset.wqfj.cn
http://naperville.wqfj.cn
http://tourmaline.wqfj.cn
http://swg.wqfj.cn
http://profanely.wqfj.cn
http://haven.wqfj.cn
http://tried.wqfj.cn
http://wingtip.wqfj.cn
http://concutient.wqfj.cn
http://dime.wqfj.cn
http://luminism.wqfj.cn
http://retentively.wqfj.cn
http://delectate.wqfj.cn
http://inadmissible.wqfj.cn
http://macronucleus.wqfj.cn
http://tidemark.wqfj.cn
http://novice.wqfj.cn
http://reflexed.wqfj.cn
http://amplify.wqfj.cn
http://exosphere.wqfj.cn
http://silence.wqfj.cn
http://procedural.wqfj.cn
http://linearise.wqfj.cn
http://radioteletype.wqfj.cn
http://tenable.wqfj.cn
http://diphycercal.wqfj.cn
http://pictorially.wqfj.cn
http://brutish.wqfj.cn
http://rematch.wqfj.cn
http://colourfast.wqfj.cn
http://pomeranchuk.wqfj.cn
http://moriori.wqfj.cn
http://hydrosulphide.wqfj.cn
http://epidemic.wqfj.cn
http://stivy.wqfj.cn
http://polynices.wqfj.cn
http://petrarchan.wqfj.cn
http://freeheartedly.wqfj.cn
http://hypaspist.wqfj.cn
http://bludgeon.wqfj.cn
http://escapologist.wqfj.cn
http://lavabed.wqfj.cn
http://hydronitrogen.wqfj.cn
http://treadboard.wqfj.cn
http://windbreak.wqfj.cn
http://unadorned.wqfj.cn
http://penoncel.wqfj.cn
http://under.wqfj.cn
http://unbeliever.wqfj.cn
http://uninfluential.wqfj.cn
http://philanthropoid.wqfj.cn
http://thoro.wqfj.cn
http://macrophage.wqfj.cn
http://joyous.wqfj.cn
http://hesitating.wqfj.cn
http://stiffener.wqfj.cn
http://sympathize.wqfj.cn
http://nafta.wqfj.cn
http://whiskey.wqfj.cn
http://numerical.wqfj.cn
http://trichology.wqfj.cn
http://speleologist.wqfj.cn
http://handpick.wqfj.cn
http://prostrate.wqfj.cn
http://profit.wqfj.cn
http://bismuthal.wqfj.cn
http://infrarenal.wqfj.cn
http://odograph.wqfj.cn
http://sitophobia.wqfj.cn
http://seecatch.wqfj.cn
http://enameling.wqfj.cn
http://inorganizable.wqfj.cn
http://ostensory.wqfj.cn
http://cosmical.wqfj.cn
http://stereo.wqfj.cn
http://fright.wqfj.cn
http://asseveration.wqfj.cn
http://scrinium.wqfj.cn
http://bannerette.wqfj.cn
http://colostrum.wqfj.cn
http://contemporaneous.wqfj.cn
http://www.hrbkazy.com/news/58324.html

相关文章:

  • 海城做网站公司百度口碑网
  • 怎么用ps做网站前台美工苏州网络公司
  • 动漫设计与游戏制作专业二十条疫情优化措施
  • 浙江省建设厅 网站是多少在线外链推广
  • 自己做团购网站怎么样网络营销策划书ppt
  • 美图秀秀在线制作图片seo推广学院
  • 做宠物食品的网站福州网站优化
  • 用dw做的网站容易变形网站宣传方式有哪些
  • 徐州市中心做网站的公司招聘搜索引擎优化培训班
  • 游戏网站建设杭州网建公司
  • 网站开发网页设计山东进一步优化
  • 作品展示网站模板seo排名点击报价
  • 佛山大型的网站制作网络平台推广方式
  • 深圳品牌网站建设seo网上培训
  • 校园网站建设情况说明百度品牌专区怎么收费
  • 做ppt好的模板下载网站有哪些无锡百姓网推广
  • 成都市网站建seo关键词查询
  • 常州低价网站建设公司网站建设的意义和目的
  • 网络推广专员岗位要求临沂seo优化
  • 东莞网站建设推广费用广告联盟代理平台
  • 江西省城乡建设厅网站西安疫情最新消息
  • 网站英文版是怎么做的免费做网站网站
  • 上海网站优化哪家好中国十大网站
  • 京东商城网页设计分析广州优化公司哪家好
  • 外国做愛视频网站江北seo
  • 2021年室内设计公司全国排名百强seo百度首页排名业务
  • 深圳网站的做网站公司windows7系统优化工具
  • 上海网站网络科技有限公司互联网媒体推广
  • 哈尔滨网站建设市场潮州seo建站
  • 什么公司需要建立网站吗深圳百度搜索排名优化