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.
相关推荐
基于RT-Thread的STM32F4开发第二讲第一篇——ADC
极其详细的基于RT-Thread studio的使用STM32F4系类开发板的ADC工程创建,一定值得一看
一篇文章让你学会Java之Thread类(线程)
线程(Thread)是程序执行的最小单位。一个Java程序默认至少有一个主线程,即main线程,负责执行main()方法。一个进程(Process)可以包含多个线程,多个线程可以共享进程中的资源(如内存、文件句柄等)。Java使用Thread类和Runnable接口来实现多线程编程。
基于RT-Thread的STM32G4开发第二讲第二篇——ADC
本文使用的是RT-Thread最新的驱动5.1.0,开发板是蓝桥杯嵌入式国信长安的开发板,MCU是STM32G431RBT6。有非常关键的ADC驱动函数编写和设置。
【我与RT-Thread的故事】我想和你深发展(RT-Thread技术社区优秀文章)
本文介绍了一位一线开发者与RT-Thread从相遇开始,再到相识、相知、相爱,最后到相守的“爱情”故事,其中经历的点点滴滴都记录成文,文章的最后还表达了对国产芯片及国产操作系统的期许,许下了为中华崛起的美好梦想而奋斗的宏伟愿望。...
onnxruntime-gpu版本选择与CUDA环境配置实战指南
本文是onnxruntime-gpu版本选择与CUDA环境配置的实战指南。针对AI模型部署中常见的GPU无法调用问题,核心在于确保onnxruntime-gpu版本与系统CUDA Toolkit版本严格匹配。文章详细讲解了如何精准确定CUDA版本、查找官方对应关系、执行正确安装命令,并通过完整测试脚本验证GPU是否成功启用,最后提供了常见问题排查与生产环境调优建议。
python语言下使用opencv接口cv2.VideoCapture()接口调用海康机器人工业相机
文章目录 目录 文章目录 前言 一、opencv接口介绍 二、调用海康机器人工业相机 1.使用海康机器人工业相机的前提 2.调用海康机器人工业相机进行显示 3.调用海康机器人工业相机进行存图 4.调用海康机器人工业相机存视频 5.调用海康机器人工业相机进行简单图像处理 总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下.
Thread、ThreadLocal、ThreadLocalMap是什么?看这一篇
一提到ThreadLocal,很多人都会心惊胆战,因为ThreadLoca确实算是JAVA并发编程中一大难点,尤其是ThreadLocal、ThreadLocalMap、Thread三者之间的关系更是错综复杂。本文结合JAVA源码,皆以最简单的方式介绍这三者概念以及它们之间的关系,希望能帮读者打通这其中的任督二脉。 之前一直有个观念,学习一个新的知识之前,必先了解其为什么诞生以及其解决的是什么问题。之前我们学习多线程的时候就知道,多线程对共享变量进行访问,可以会出现安全问题。 ThreadLocal(线程变
Zephyr RTOS详解
Zephyr 是一个开源的实时操作系统(RTOS),专为资源受限的嵌入式设备设计,支持多种架构(如 ARM Cortex-M、RISC-V、x86 等)。
一篇介绍state thread库的好文章
原文地址为:http://coolshell.cn/articles/12012.html 我复制黏贴下 上回写了篇《一个“蝇量级”C语言协程库》,推荐了一下Protothreads,通过coroutine模拟了用户级别的multi-threading模型,虽然本身足够“轻”,杜绝了系统开销,但这个库本身应用场合主要是内存限制的嵌入式领域,提供原生态组件太少,使用限制太多,比如
YOLOv11改进策略【模型轻量化】| 替换骨干网络 CVPR-2023 FasterNet 高效快速的部分卷积块
本文记录的是基于FasterNet的YOLOv11轻量化改进方法研究。FasterNet的网络结构借鉴 CNN 的设计理念,通过提出的PConv减少推理时的计算和内存成本,同时减少通道数并增加部分比例,降低延迟,并通过后续的PWConv来弥补特征信息可能缺失的问题,提高了准确性。本文在替换骨干网络中配置了原论文中的和六种模型,以满足不同的需求。
Claude Code 从入门到精通,通过实战项目穿插 Agent Skills、MCP、Hooks、子代理、插件等十五个知识点
本文介绍了Claude Code的高级使用技巧,通过一个个人简历网站实战项目,详细讲解了15个核心知识点。主要内容包括:三种权限模式(默认/编辑/计划)的使用与切换;历史会话管理(/resume、/rename、/clear命令);会话回滚功能(/rewind);以及危险参数--dangerously-skip-permissions的注意事项。文章还演示了如何通过计划模式拆分代码文件,并强调了在复杂任务中仔细检查规划步骤的重要性。这些技巧能帮助开发者更高效地使用Claude Code完成项目开发。
RT-Thread零基础快速入门第1讲——新建工程
RT-Thread零基础快速入门第2讲——新建工程 目录RT-Thread零基础快速入门第2讲——新建工程一、前言二、使用ENV工具新建项目工程使用env生成工程使用STM32CubeMX配置底层驱动三、使用RT-Thread Studio新建项目工程四、在原有的工程上移植RT-thread 一、前言 这一讲我来给大家介绍几种常用的新建工@TOC程的方法,在开始之前,我们需要先安装好RT-thre...
【ROS2实体机械臂驱动】封装珞石ROS2驱动库ros2_rokae_driver_lib
这一篇文章将封装为,和之前一篇文章《ROS2 编译、打包、封装三方库(Knet为例)_ros插件编译成静态库-CSDN博客》写的封装,区别在于优化了cmake dependencies和include等属性和宏定义,同时对于多库内部依赖的情况做了特定处理,更加方便外部调用。其中珞石的编译参考博主之前的文章rokae xCoreSDK C++测试使用,这里不再做赘述。通过这篇文章对于如何打包机械臂厂家提供的三方库做了进一步说明,方便用于不同机械臂厂家的三方库封装调用。!!
Java Thread 很不错的一篇文章
如何停止java的线程一直是一个困恼我们开发多线程程序的一个问题。这个问题最终在Java5的java.util.concurrent中得到了回答:使用interrupt(),让线程在run方法中停止。 简介 在Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(),stop(),stop(Throwable) and suspend(),
STM32实战开发(66):STM32图像处理开发教程
随着物联网、人工智能和嵌入式技术的不断发展,图像处理技术在嵌入式系统中的应用越来越广泛。图像处理不仅在视觉识别、安防监控、智能家居等领域中发挥着重要作用,也在工业自动化、医疗成像、无人驾驶等高科技领域中占据重要地位。而STM32凭借其强大的硬件性能和丰富的外设支持,成为了许多图像处理应用的理想平台。
某验SVG图标验证码识别思路(非训练)
本文提出了一种无需训练的某验SVG图标验证码识别方法。通过分析SVG文件结构,使用正则表达式提取静态图标数据,并借助CairoSVG库转换为PNG格式。随后采用坐标定位方式裁剪出12个固定位置的图标,最后利用ResNet50提取特征,通过余弦相似度匹配目标图标。该方法避免了训练目标检测模型,实现了高效准确的验证码识别。关键步骤包括SVG文件预处理、图标定位裁剪和基于预训练模型的相似度比对。
基于RT-Thread的STM32开发第一讲——USART
之前本人一直都是使用keil搭配STM32cubemx使用LL库来开发STM32,但随着使用,发现裸机编程对于集成大的工程越来越费劲,经常几天不看,自己都忘了为啥这么写。于是就想使用操作系统来开发。于是我就去了解嵌入式系统中常用的实时操作系统(RTOS),发现使用较多是以下三种——RT-Thread、FreeRTOS、uC/OS。我并没有过多去比较它们的优缺点,就冲着RT-Thread是唯一国产的且开源,而且是最晚面世的(本人就喜欢用新东西),就选择它了。这里说一下对于没接触过操作系统如何开始学习。
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
一篇文章带你搞定 Java 中多线程的实现(Thread,Runnable)
Java 中要想实现多线程代码有两种手段,一种是继承 Thread 类,另一种就是实现 Runnable 接口 开发工程中,建议使用 Runnable 接口实现多线程 文章目录一、继承 Thread 类二、实现 Runnable 接口三、Thread 类和 Runnable 接口 一、继承 Thread 类 Thread 类是在 java.lang 包中定义的,一个类只要继承了 Thread 类,...
LinePhone3.2.7(android端最新版本) sip客户端
SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/
NTscan扫描工具
ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。
RT-Thread零基础快速入门第9讲——串口(UART/RS485)
串口是单片机最常用的通讯方式之一,关于串口的介绍在RT-thread官网上已经有非常详细的介绍了,我这里就不多讲了,今天主要讲一讲官网上没有的东西,让你更加深入的了解RT-thread是如何配置和使用串口进行数据收发的。
老版QQ原生表情gif动态
这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。
基于RT-Thread的STM32F4开发第三讲——DAC
本章利用RT-Thread最新的驱动5.1.0开发DAC模块,使用的开发板是正点原子的STM32F4探索者。很多配置和上文重复,本文不再重复叙述。
基于RT-Thread的STM32开发第十讲——CAN通讯
本文介绍了基于RT-Thread和STM32F103C8T6的CAN通讯开发。
几篇关于Thread与AsyncTask的解析文章
转载自:http://blog.csdn.net/easonx1990/article/details/7997521 AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主线程. 使用的优点
有关 Thread.Sleep(2000),写的很好
学习source,看到了下面一段代码 try { Thread.sleep(2000); } catch (InterruptedException e) { } 首先这段代码的作用是使当前进程沉睡2S,展现给用户的结果就是画面维持两秒,有个“正在启动”的感觉而已。 其实,之前还有看到过sleep(0)的时候,这又是什么作用呢? 百度之,搜到一
详讲Thread类及其常见方法
文章目录1.1Thread类的常见方法1.2Thread的常见属性1.3启动一个线程-start()start()和run()方法的区别1.4中断一个线程1.5等待一个线程-join()1.6获取当前线程引用1.7休眠当前线程 Thread类是jvm用来管理线程的一个类,换句话说,也就是每个线程都有唯一的Thread对象与之关联。 1.1Thread类的常见方法 //简单示例 Thread t1=new Thread(); Thread t2=new Thread(new Runnable()
嵌入式开发常用的Ring Buffer,今天一篇文章讲透它
因为缓冲区成头尾相连的环形,写操作可能会覆盖未及时读取的数据,有的场景允许这种情况发生,有的场景又严格限制这种情况发生。因为缓冲区成头尾相连的环形,写操作可能会覆盖未及时读取的数据,有的场景允许这种情况发生,有的场景又严格限制这种情况发生。来看策略一,例如,这时。如果缓冲区的大小需要经常调整,就不适合用环形缓冲区,因为在扩展缓冲区大小时,需要搬移其中的数据,这种场合使用链表更加合适;如果缓冲区的大小需要经常调整,就不适合用环形缓存区,因为在扩展缓冲区大小时,需要搬移其中的数据,这种场合使用链表更加合适;
Fluent 中Domain和thread的个人理解
简单理解,Domain的级别要比Thread的级别高。先看这个汇总。
1108



被折叠的 条评论
为什么被折叠?



