一篇很好的讲Thread的文章

FROM: http://www.j2me.org/yabbse/index.php?board=10;action=display;threadid=2482

J2ME and Multithreading
« on: September 17, 2004, 04:40:54 am »

--------------------------------------------------------------------------------
J2ME and multithreading mini-article

Introduction

The scope of this article is to give a brief overview of how threading works in Java with a particular focus on J2ME. My reason for writing this is to address some misconceptions people may have.

Now, getting straight to it...


Java doesn't do threads.

Java itself does not offer any threading functionality. It merely abstracts the functionality found in the OS. Because of this fact, the behavior of threading in Java is said to be "non-deterministic". In other words, Java makes no guarantees as to how multithreading will be accomplished.

There is no such thing as 2 threads running at the same time on a single CPU device

Obviously in J2ME devices there is only one processor so the appearance that many threads are running simultaneously is incorrect. Only one thread is ever running at a time. This means having two threads executing 1 job each is actually slower than having one thread perform the same 2 jobs. Why? Because nothing comes for free and there is management overhead when switching threads.

About Cooperative Multitasking

Your J2ME device's OS is most probably running a simplistic cooperative multitasker. I say probably because I don't know what handset you are using but I know that if it had the power to do preemptive multitasking it would probably run J2SE and not J2ME.
What's the difference? Well, with preemptive multitasking the CPU gives a slice of time to each thread so that each gets a chance to execute. With cooperative multitasking each thread either terminates and relinquishes the CPU or has to explicitly yield control of the CPU over. So what does that mean? It means having 10 balls bouncing around, each with its own thread really makes one ball bounce around at a time since one thread is active at a time. Remember, each ball's thread continues until it dies, then the next ball gets its chance (in the worst case scenario of 100% cooperative multitasking).

So what if I yield each of my 10 threads every xxxx milliseconds? Will it make the balls bounce more smoothly?

Possibly, it depends on the OS' thread scheduler. But this much is for certain... All the work for saving thread A's context and retrieving thread B's context every xxxx milliseconds means your balls will bounce sloooooowwwwly. The CPU might even spend more time managing thread contexts than it does running the threads themselves.

The threads that are already running

Whether you know it or not your application is running its own "main" thread, all your key events are being captured on another thread, and your painting operations occur on yet another thread.

Be certain that your application needs another thread before implementing it!

When is a good time to use threads then?

Multiple threads are useful when you want to allow 2 things to occur independently. For instance, you may want to monitor and inform the progress of an operation like loading graphics from your JAR file.

When is a bad time to use threads?

Avoid thread use for animation. Conduct the animation in your main game loop. An animation is like a flip-book where each static image is displayed in sequence to provide the illusion of movement. Your main game loop is perfect for that task. It keeps refreshing the screen for you so just prepare the next image and have it displayed in sync with the next refresh.

Conversely, if you were to use another thread to control the animation you might encounter race conditions where the animation thread and the main thread (where you're game logic resides) touch the same image object. You'd have to synchronize your code to avoid these situations but synchronized code runs 4x slower than normal code so you're really just shooting yourself in the foot.

Common Pitfalls

Here is a compact list of facts you should know about Threads...

- Setting the priority of a thread does not guarantee that the thread will have more or less of a priority over other threads. The priority number you assign may not even map to the OS's priority system if it has one at all.

- Calling Thread.sleep(1000) will cause the thread to sleep at least 1000 milliseconds and perhaps more. The thread scheduler takes time to bring the thread back to life and has to consider other running threads' contexts as well. The main takeaway here is to not count on sleep being an accurate time keeper if your logic requires precision. Speaking from experience, most of my Threads that sleep for 1000 milliseconds actually come back to life 1010 milliseconds later in my emulator.

- Each thread has its own call stack and that means any thread can cause the application to crash if exceptions are not properly caught in each run() method.

- Synchronized code runs 4 times slower than non-synchronized code. Avoid it unless there is a possiblity of a race condition occuring in the code.

Conclusion

Threads don't necessarily make your code run faster and in many cases can even slow down your code. How Threads behave varies between devices and the emulator and there is no guarantee of how Threads will be scheduled. There is an appropriate time to use Threads but in most cases they are not needed and should be avoided.

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值