本文介绍了单线程到多线程应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时应在应用程序中使用线程.换句话说,何时应该将单线程应用程序转换为多线程应用程序.作为开发人员,我认为阻止您的应用程序顺利运行的任务.该任务可以由线程处理.就像我们正在不断获取GPS数据一样.我认为,还有其他更多原因可以在您的应用程序中创建线程.请分享您的意见.

When we should use threads in our application. In other words, when should I convert a single threaded application to multi-threaded application.Being a developer, I think the task which is preventing to your application to run smoothly. That task can be handled by a thread. Like we are the getting GPS data Continuously.I think, there are other more reasons to create thread in your application. Please share your opinion.

谢谢.

推荐答案

原因我可以想到的是(
我确信还有更多):
1.将一批工作分担给工作线程,以便您的程序可以继续响应用户输入,或者可以继续运行不依赖于该工作的其他代码.
2.处理I/O,特别是响应时间可变或未知的服务器和网络通信.
3.并行处理数据,您可以在其中将工作细分为离散的非相关工作单元
4.与计时器相关的工作,即每500毫秒检查一次x是否已更改"

Reasons I can think of off the top of my head ( and I'm sure there are more ) are :
1. Offloading a batch of work to a worker thread so your program can either continue responding to user input or so you can carry on running other code that doesn't rely on that work.
2. Handling I/O particularly server and network communication where the response time is variable or unknown.
3. Parallel processing of data where you can sub-divide the work down into discrete non-dependant units of work
4. Timer related work i.e 'every 500ms check if x has changed'

但是,切换到多线程或并发编程并非没有陷阱,特别是如果那些线程需要访问共享数据时,那么关于互斥体线程同步

However, switching to multi-threaded or concurrent programming is not without it's pitfalls particularly if those threads need to access shared data hence the number of questions on SO about mutexes and thread synchronisation!

这篇关于单线程到多线程应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!