- 浏览: 198373 次
- 性别:
- 来自: 上海
文章分类
最新评论
Making a Thread
A thread in Java begins as an instance of java.lang.Thread. For the exam, you’ll need to know, at a minimum, the following methods:
start()
yield()
sleep()
run()
You can define and instantiate a thread in one of two ways:
■ Extend the java.lang.Thread class
■ Implement the Runnable interface
方法一:Extending java.lang.Thread
The simplest way to define code to run in a separate thread is to
■ Extend the Thread class.
■ Override the run() method.
下面的例子示例了通过此方法实现的一个thread:
方法二:Implementing java.lang.Runnable
通过实现java.lang.Runnable接口,亦可以定义一个thread,示例如下:
注意:因为是implements一个interface,因此你必须实现其public void run();方法(Runnable接口只有这一个方法)
对应于Define a thread的两种方法,Instantiating a thread也分别有两种方法:
方法一
如果你的thread是通过extends java.lang.Thread的方法获得,那么实例化一个Thread就非常简单:
MyThread t = new MyThread(); //这里的MyThread extends Thread的class
方法二
如果你的thread是通过implements Runnable接口的方法获得,那么实例化一个thread就稍微复杂一些,分为两个步骤:
1.MyRunnable r = new MyRunnable(); //这里的MyRunnable是implements Runnable的class
2.Thread t = new Thread(r); //将你的Runnable class传递给一个java.lang.Thread class
//因为只有java.lang.Thread才有用来启动线程的start()
注意:你可以将一个Runnable传递给多个Thread实例,例如:
上例中:三个Thread运行的任务是独立(三个Thread各自的call stack)且相同的(任务内容是一样的)。
你必须知道Thread的其它形式的构造函数
■ Thread()
■ Thread(Runnable target)
■ Thread(Runnable target, String name)
■ Thread(String name)
■ Thread(ThreadGroup group, Runnable target)
■ Thread(ThreadGroup group, Runnable target, String name)
■ Thread(ThreadGroup group, String name)
Thread的几个有用的方法:
1. public static Thread currentThread() :Returns a reference to the currently executing thread object.
2. public final void setName(String name):Changes the name of this thread to be equal to the argument name.
3. public final String getName():Returns this thread's name.
Methods from the java.lang.Thread Class Some of the methods that can help us influence thread scheduling are as follows:
Note that both sleep() and join() have overloaded versions not shown here.
Methods from the java.lang.Object Class Every class in Java inherits the following three thread-related methods:
The wait() method has three overloaded versions (including the one listed here).
Thread States
A thread can be only in one of five states
■ New This is the state the thread is in after the Thread instance has been instantiated, but the start() method has not been invoked on the thread. It is a live Thread object, but not yet a thread of execution. At this point, the thread is considered not alive. 线程实例(java.lang.thread或者其subclass)对象被创建,但是其start()方法还没有被调用。此时线程还不是活动的(alive).
■ Runnable This is the state a thread is in when it’s eligible to run, but the scheduler has not selected it to be the running thread. A thread first enters the runnable state when the start() method is invoked, but a thread can also return to the runnable state after either running or coming back from a blocked, waiting, or sleeping state. When the thread is in the runnable state, it is considered alive. 此时线程已经满足了所有运行所需的条件,只等到thread scheduler选中它就可以运行了。线程第一次进入Runnable状态的标志是,该线程的start()方法被调用。但是这并不是线程进入Runnable状态的唯一方法,处于Running状态和Blocking状态的线程也可以返回到Runnable状态。此时线程是活动的(alive).
■ Waiting/blocked/sleeping OK, so this is really three states combined into one, but they all have one thing in common: the thread is still alive, but is currently not eligible to run. In other words, it is not runnable, but it might return to a runnable state later if a particular event occurs. 1. A thread may be blocked waiting for a resource (like I/O or an object’s lock), in which case the event that sends it back to runnable is the availability of the resource—for example, if data comes in through the input stream the thread code is reading from, or if the object’s lock suddenly becomes available. 2. A thread may be sleeping because the thread’s run code tells it to sleep for some period of time, in which case the event that sends it back to runnable is that it wakes up because its sleep time has expired. 3.Or the thread may be waiting, because the thread’s run code causes it to wait, in which case the event that sends it back to runnable is that another thread sends a notification that it may no longer be necessary for the thread to wait. The important point is that one thread does not tell another thread to block. 也就是说,导致线程Blocking/Sleeping/Waiting的原因都是线程自身代码运行中遇到的,而其它的Thread并不能够通知/告诉/要求当前运行的线程被Block。There is a method, suspend(), in the Thread class, that lets one thread tell another to suspend, but the suspend() method has been deprecated and won’t be on the exam (nor will its counterpart resume()). There is also a stop() method, but it too has been deprecated and we won’t even go there. Both suspend() and stop() turned out to be very dangerous, so you shouldn’t use them and again, because they’re deprecated, they won’t appear on the exam. Don’t study ‘em, don’t use ‘em. Note also that a thread in a blocked state is still considered to be alive. 值得注意的是,处于Blocking状态的thread仍然是活动的(alive).
■ Dead A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution. Once a thread is dead, it can never be brought back to life! (The whole “I see dead threads” thing.) If you invoke start() on a dead Thread instance, you’ll get a runtime (not compiler) exception. And it probably doesn’t take a rocket scientist to tell you that if a thread is dead, it is no longer considered to be alive.
Thread Priorities and Yield
要知道,不同的JVM实现的线程调度机制不同,有的实行优先级抢占策略,有的实行时间片轮转策略。
通常每个thread都有自己的优先级—priority。一般priority用1到10的数字来表示。
注意:Thread类有以下关于线程priority的static final常量:
public static final int MAX_PRIORITY
The maximum priority that a thread can have.
public static final int MIN_PRIORITY
The minimum priority that a thread can have.
public static final int NORM_PRIORITY
The default priority that is assigned to a thread.
Setting a Thread’s Priority
A thread gets a default priority that is the priority of the thread of execution that creates it.
默认地,一个线程的优先级是和创建它的线程相同的。例如:
the thread referenced by t 和 the main thread具有相同的优先级, since the main thread is executing the code that creates the MyThread instance.
当然你也可以通过Thread实例的public final void setPriority(int newPriority)方法来为一个线程设定优先级。例如:
The default priority is 5
关于yield()方法
也就是说,Thread的yield()方法的作用就是让当前线程暂时停止执行,返回到Runnable状态。以便其它的与之具有相同优先级的处于Runnable状态的线程可以获得调度。
但是这一原则性的策略的执行并不会得到JVM的保证。因为,yield()虽然可以保证使得当前正在执行的线程进入Runnable状态,但是并不会保证进入Runnable的该线程不会被thread scheduler再次选中,重新进入Running状态。因为,被yield()方法放进Runnable Pool的该进程会和Runnable Pool中具有相同优先级的进程一样被thread scheduler来考量
The Join() Method
1.public final void join() throws InterruptedException
Waits for this thread to die
2.public final void join(long millis) throws InterruptedException
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
3.public final void join(long millis,int nanos) throws InterruptedException
Waits at most millis milliseconds plus nanos nanoseconds for this thread to die
Synchronizing Code
Write code using synchronized wait, notify, and notifyAll to protect against concurrent access problems and to communicate between threads.
So how do you protect the data? You must do two things:
■ Mark the variables private
■ Synchronize the code that modifies the variables
关于lock和synchronized我们应该记住下面的要点:
■ Only methods can be synchronized, not variables.
只有method可以被synchronized,变量不可以
■ Each object has just one lock.
每个对象只有一个机锁
■ Not all methods in a class must be synchronized. A class can have both synchronized and nonsynchronized methods.
一个class中并非所有的method都必须是synchronized的。一个class可以有synchronized方法,也可以有nonsynchronized方法
■ If two methods are synchronized in a class, only one thread can be accessing one of the two methods. In other words, once a thread acquires the lock on an object, no other thread can enter any of the synchronized methods in that class (for that object).
机锁具有独占性,一旦被一个Thread获得,其他的thread就不能够再拥有。
■ If a class has both synchronized and nonsynchronized methods, multiple threads can still access the nonsynchronized methods of the class! If you have methods that don’t access the data you’re trying to protect, then you don’t need to mark them as synchronized. Synchronization is a performance hit, so you don’t want to use it without a good reason.
Nonsynchronized方法并不会导致对象的lock被激活。但是过多的使用synchronized method会影响性能,所以要慎用。
■ If a thread goes to sleep, it takes its locks with it.
一个线程进入Sleeping状态,他不会释放他所拥有的其它对象的lock
■ A thread can acquire more than one lock.
一个线程可以同时获得多个对象的机锁(lock)
常见的几个Methods小结:
java.lang.Runnable(inerface):
1.public void run()
java.lang.Thread:
java.lang.Thread implements java.lang.Runnable
1.public void start()
2.public void run() from java.lang.Runnable
3.sleep() throws InterruptedException
public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis,int nanos) throws InterruptedException
4. public static void yield()
5. join() throws InterruputedException
public final void join() throws InterruptedException
public final void join(long millis) throws InterruptedException
public final void join(long millis,int nanos) throws InterruptedException
java.lang.Object
1. Wait() throws InterruptedException,必须在synchronized code中,否则掷出异常IllegalMonitorStateException说明必须拥有lock,注意此异常不需捕捉
public final void wait() throws InterruptedException
public final void wait(long timeout) throws InterruptedException
public final void wait(long timeout,int nanos) throws InterruptedException
2. notify(),否则掷出异常IllegalMonitorStateException说明必须拥有lock,注意此异常不需捕捉
public final void notify()
3. notifyAll(),否则掷出异常IllegalMonitorStateException说明必须拥有lock,注意此异常不需捕捉
public final void notifyAll()
A thread in Java begins as an instance of java.lang.Thread. For the exam, you’ll need to know, at a minimum, the following methods:
start()
yield()
sleep()
run()
You can define and instantiate a thread in one of two ways:
■ Extend the java.lang.Thread class
■ Implement the Runnable interface
方法一:Extending java.lang.Thread
The simplest way to define code to run in a separate thread is to
■ Extend the Thread class.
■ Override the run() method.
下面的例子示例了通过此方法实现的一个thread:
class MyThread extends Thread { public void run() { System.out.println("Important job running in MyThread"); } }
方法二:Implementing java.lang.Runnable
通过实现java.lang.Runnable接口,亦可以定义一个thread,示例如下:
class MyRunnable implements Runnable { public void run() { System.out.println("Important job running in MyRunnable"); } }
注意:因为是implements一个interface,因此你必须实现其public void run();方法(Runnable接口只有这一个方法)
对应于Define a thread的两种方法,Instantiating a thread也分别有两种方法:
方法一
如果你的thread是通过extends java.lang.Thread的方法获得,那么实例化一个Thread就非常简单:
MyThread t = new MyThread(); //这里的MyThread extends Thread的class
方法二
如果你的thread是通过implements Runnable接口的方法获得,那么实例化一个thread就稍微复杂一些,分为两个步骤:
1.MyRunnable r = new MyRunnable(); //这里的MyRunnable是implements Runnable的class
2.Thread t = new Thread(r); //将你的Runnable class传递给一个java.lang.Thread class
//因为只有java.lang.Thread才有用来启动线程的start()
注意:你可以将一个Runnable传递给多个Thread实例,例如:
public class TestThreads { public static void main (String [] args) { MyRunnable r = new MyRunnable(); //带有任务(run()方法)的Runnable Thread foo = new Thread(r); //创建了三个Thread Thread bar = new Thread(r); Thread bat = new Thread(r); } }
上例中:三个Thread运行的任务是独立(三个Thread各自的call stack)且相同的(任务内容是一样的)。
你必须知道Thread的其它形式的构造函数
■ Thread()
■ Thread(Runnable target)
■ Thread(Runnable target, String name)
■ Thread(String name)
■ Thread(ThreadGroup group, Runnable target)
■ Thread(ThreadGroup group, Runnable target, String name)
■ Thread(ThreadGroup group, String name)
Thread的几个有用的方法:
1. public static Thread currentThread() :Returns a reference to the currently executing thread object.
2. public final void setName(String name):Changes the name of this thread to be equal to the argument name.
3. public final String getName():Returns this thread's name.
Methods from the java.lang.Thread Class Some of the methods that can help us influence thread scheduling are as follows:
public static void sleep(long millis) throws InterruptedException public static void yield() public final void join() throws InterruptedException public final void setPriority(int newPriority)
Note that both sleep() and join() have overloaded versions not shown here.
Methods from the java.lang.Object Class Every class in Java inherits the following three thread-related methods:
public final void wait() throws InterruptedException public final void notify() public final void notifyAll()
The wait() method has three overloaded versions (including the one listed here).
Thread States
A thread can be only in one of five states
■ New This is the state the thread is in after the Thread instance has been instantiated, but the start() method has not been invoked on the thread. It is a live Thread object, but not yet a thread of execution. At this point, the thread is considered not alive. 线程实例(java.lang.thread或者其subclass)对象被创建,但是其start()方法还没有被调用。此时线程还不是活动的(alive).
■ Runnable This is the state a thread is in when it’s eligible to run, but the scheduler has not selected it to be the running thread. A thread first enters the runnable state when the start() method is invoked, but a thread can also return to the runnable state after either running or coming back from a blocked, waiting, or sleeping state. When the thread is in the runnable state, it is considered alive. 此时线程已经满足了所有运行所需的条件,只等到thread scheduler选中它就可以运行了。线程第一次进入Runnable状态的标志是,该线程的start()方法被调用。但是这并不是线程进入Runnable状态的唯一方法,处于Running状态和Blocking状态的线程也可以返回到Runnable状态。此时线程是活动的(alive).
■ Waiting/blocked/sleeping OK, so this is really three states combined into one, but they all have one thing in common: the thread is still alive, but is currently not eligible to run. In other words, it is not runnable, but it might return to a runnable state later if a particular event occurs. 1. A thread may be blocked waiting for a resource (like I/O or an object’s lock), in which case the event that sends it back to runnable is the availability of the resource—for example, if data comes in through the input stream the thread code is reading from, or if the object’s lock suddenly becomes available. 2. A thread may be sleeping because the thread’s run code tells it to sleep for some period of time, in which case the event that sends it back to runnable is that it wakes up because its sleep time has expired. 3.Or the thread may be waiting, because the thread’s run code causes it to wait, in which case the event that sends it back to runnable is that another thread sends a notification that it may no longer be necessary for the thread to wait. The important point is that one thread does not tell another thread to block. 也就是说,导致线程Blocking/Sleeping/Waiting的原因都是线程自身代码运行中遇到的,而其它的Thread并不能够通知/告诉/要求当前运行的线程被Block。There is a method, suspend(), in the Thread class, that lets one thread tell another to suspend, but the suspend() method has been deprecated and won’t be on the exam (nor will its counterpart resume()). There is also a stop() method, but it too has been deprecated and we won’t even go there. Both suspend() and stop() turned out to be very dangerous, so you shouldn’t use them and again, because they’re deprecated, they won’t appear on the exam. Don’t study ‘em, don’t use ‘em. Note also that a thread in a blocked state is still considered to be alive. 值得注意的是,处于Blocking状态的thread仍然是活动的(alive).
■ Dead A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution. Once a thread is dead, it can never be brought back to life! (The whole “I see dead threads” thing.) If you invoke start() on a dead Thread instance, you’ll get a runtime (not compiler) exception. And it probably doesn’t take a rocket scientist to tell you that if a thread is dead, it is no longer considered to be alive.
Thread Priorities and Yield
要知道,不同的JVM实现的线程调度机制不同,有的实行优先级抢占策略,有的实行时间片轮转策略。
通常每个thread都有自己的优先级—priority。一般priority用1到10的数字来表示。
注意:Thread类有以下关于线程priority的static final常量:
public static final int MAX_PRIORITY
The maximum priority that a thread can have.
public static final int MIN_PRIORITY
The minimum priority that a thread can have.
public static final int NORM_PRIORITY
The default priority that is assigned to a thread.
Setting a Thread’s Priority
A thread gets a default priority that is the priority of the thread of execution that creates it.
默认地,一个线程的优先级是和创建它的线程相同的。例如:
public class TestThreads { public static void main (String [] args) { MyThread t = new MyThread(); } }
the thread referenced by t 和 the main thread具有相同的优先级, since the main thread is executing the code that creates the MyThread instance.
当然你也可以通过Thread实例的public final void setPriority(int newPriority)方法来为一个线程设定优先级。例如:
FooRunnable r = new FooRunnable(); Thread t = new Thread(r); t.setPriority(8); //为Thread t设定的优先级为8 t.start();
The default priority is 5
关于yield()方法
也就是说,Thread的yield()方法的作用就是让当前线程暂时停止执行,返回到Runnable状态。以便其它的与之具有相同优先级的处于Runnable状态的线程可以获得调度。
但是这一原则性的策略的执行并不会得到JVM的保证。因为,yield()虽然可以保证使得当前正在执行的线程进入Runnable状态,但是并不会保证进入Runnable的该线程不会被thread scheduler再次选中,重新进入Running状态。因为,被yield()方法放进Runnable Pool的该进程会和Runnable Pool中具有相同优先级的进程一样被thread scheduler来考量
The Join() Method
1.public final void join() throws InterruptedException
Waits for this thread to die
2.public final void join(long millis) throws InterruptedException
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
3.public final void join(long millis,int nanos) throws InterruptedException
Waits at most millis milliseconds plus nanos nanoseconds for this thread to die
Synchronizing Code
Write code using synchronized wait, notify, and notifyAll to protect against concurrent access problems and to communicate between threads.
So how do you protect the data? You must do two things:
■ Mark the variables private
■ Synchronize the code that modifies the variables
关于lock和synchronized我们应该记住下面的要点:
■ Only methods can be synchronized, not variables.
只有method可以被synchronized,变量不可以
■ Each object has just one lock.
每个对象只有一个机锁
■ Not all methods in a class must be synchronized. A class can have both synchronized and nonsynchronized methods.
一个class中并非所有的method都必须是synchronized的。一个class可以有synchronized方法,也可以有nonsynchronized方法
■ If two methods are synchronized in a class, only one thread can be accessing one of the two methods. In other words, once a thread acquires the lock on an object, no other thread can enter any of the synchronized methods in that class (for that object).
机锁具有独占性,一旦被一个Thread获得,其他的thread就不能够再拥有。
■ If a class has both synchronized and nonsynchronized methods, multiple threads can still access the nonsynchronized methods of the class! If you have methods that don’t access the data you’re trying to protect, then you don’t need to mark them as synchronized. Synchronization is a performance hit, so you don’t want to use it without a good reason.
Nonsynchronized方法并不会导致对象的lock被激活。但是过多的使用synchronized method会影响性能,所以要慎用。
■ If a thread goes to sleep, it takes its locks with it.
一个线程进入Sleeping状态,他不会释放他所拥有的其它对象的lock
■ A thread can acquire more than one lock.
一个线程可以同时获得多个对象的机锁(lock)
常见的几个Methods小结:
java.lang.Runnable(inerface):
1.public void run()
java.lang.Thread:
java.lang.Thread implements java.lang.Runnable
1.public void start()
2.public void run() from java.lang.Runnable
3.sleep() throws InterruptedException
public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis,int nanos) throws InterruptedException
4. public static void yield()
5. join() throws InterruputedException
public final void join() throws InterruptedException
public final void join(long millis) throws InterruptedException
public final void join(long millis,int nanos) throws InterruptedException
java.lang.Object
1. Wait() throws InterruptedException,必须在synchronized code中,否则掷出异常IllegalMonitorStateException说明必须拥有lock,注意此异常不需捕捉
public final void wait() throws InterruptedException
public final void wait(long timeout) throws InterruptedException
public final void wait(long timeout,int nanos) throws InterruptedException
2. notify(),否则掷出异常IllegalMonitorStateException说明必须拥有lock,注意此异常不需捕捉
public final void notify()
3. notifyAll(),否则掷出异常IllegalMonitorStateException说明必须拥有lock,注意此异常不需捕捉
public final void notifyAll()
发表评论
-
Java 5 并发学习(转)
2012-06-26 14:38 863Java 5 并发学习 在Java5之后,并发线程这块发生 ... -
WeakHashMap和HashMap的区别
2012-02-24 13:42 845http://mzlly999.iteye.com/blog/ ... -
volatile 的高级模式
2012-02-22 13:07 736前面几节介绍的模式涵盖了大部分的基本用例,在这些模式中使用 v ... -
The "Double-Checked Locking is Broken" Declaration
2012-02-22 10:34 961http://www.cs.umd.edu/~pugh/jav ... -
Log4j配置文件详细说明[转]
2012-02-14 10:48 1346属性文件Properties properties属性文件 ... -
Log4j的配置文件
2012-02-14 10:42 817Log4j支持两种配置文件格式,一种是java属性文件(键—值 ... -
单例模式的俩种方式
2012-01-04 14:09 937等等 单例模式的俩种方式: 饿汉式 class Singlet ... -
Mysql连接数据库:PreparedStatement.addBatch()方法
2011-12-23 08:43 58211.Eclipse连接MySQL数据库 mysql>C ... -
java中ArrayList 、LinkList区别
2011-12-21 15:30 1172java中ArrayList 、LinkList、List区别 ... -
Date4j,一个简约的日期处理类库
2011-12-14 10:13 848Java本身的日期类在JDK1.0版本之后就再也没有更新过,同 ... -
How to use Log4j
2011-09-29 13:57 8131. LogManager.getInstance().get ... -
重写hashCode和equals方法(转)
2011-09-27 09:58 1136如果你的对象想散列存 ... -
学习Enum转
2011-09-22 14:11 8751. 关于 Java Enum: 学过 C/C++ 等 ... -
PO BO VO DTO POJO DAO概念及其作用(转)
2011-09-20 09:49 665J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨 ... -
Thread的实现
2011-09-20 09:47 859Thread的实现 1.extend Thread,then ... -
Adapter模式(转)
2011-08-17 15:48 890[b]GOF《设计模式》一书对Adapter模式是这样描述的: ... -
JVM常见配置汇总
2011-08-15 14:56 1905从这个图中可以看到, ... -
Abstract or Interface
2011-08-11 15:02 729详解java中的抽象类和接 ... -
HashMap HashTable TreeMap
2011-08-11 13:45 966Map中我们通过对象来对对象进行索引,用来索引的对象叫做key ... -
多线程死锁问题(转)
2011-08-10 19:42 586前天俺们谈到了加锁,但是在使用加锁的同时又会带来一个问题,就是 ...
相关推荐
在本实例"JAVA100例之实例60 继承Thread实现多线程"中,我们将探讨如何通过继承Java内置的`Thread`类来创建并运行多个线程。 首先,让我们理解什么是线程。线程是程序中的执行流,每个线程都有自己的程序计数器、栈...
继承Thread类: 1必须重写run 方法:里面放置的实际的线程体 2 启动线程: 3创建Thread对象 4调用Thread对象的start 方法启动线程
传统的多线程实现通常涉及重写`Runnable`接口的`run`方法或直接继承`Thread`类并覆盖`run`方法。然而,还有一种更高级的多线程技术,即使用`MoveToThread`函数,它可以提供更灵活且高效的方式来管理和运行线程。本文...
在本项目中,开发者利用了实时操作系统(RTOS)RT-Thread来实现两款经典小游戏——贪吃蛇和俄罗斯方块。RT-Thread是一个轻量级、高效且可扩展的开源RTOS,广泛应用于嵌入式系统中,尤其适用于资源有限的微控制器如...
应用 RT-Thread 实现蜂鸣器播放器简介这是一个应用 RT-Thread 实现蜂鸣器播放器的教程,共包含 5 节的内容,介绍了由浅入深,一步步实现一个蜂鸣器播放器的过程。此播放器支持 歌曲列表、上一曲、下一曲、暂停/播放...
毕设当初做的一个用QT实现的C/S简单多线程服务器。使用moveToThread实现多线程,同时保留句柄,分离业务操作,涉及MYSQL通讯处理,适合新手练手。
JavaFX + SQLite + Thread实现多操作系统下的文件搜索工具源码.zip
在基于RT-Thread操作系统开发的绘本故事机项目中,利用了RT-Thread提供的多种功能特性,如线程管理、信号量、消息队列和邮箱、内存管理、定时器以及设备管理等,以实现一个具有音视频播放功能的智能故事机。...
本篇文章将深入探讨C语言和Java语言中的线程实现,以及如何在用户级别进行线程调度。 首先,我们来了解线程的基本概念。线程是操作系统分配CPU时间的基本单位,每个线程都有自己的程序计数器、栈、局部变量等,但...
标题"rtthread F103C8 按键消抖程序"表明我们要讨论的是如何在RT-Thread实时操作系统环境下,为STM32F103C8的按键实现消抖功能。本文将深入探讨这个主题,解释按键消抖的原理,介绍RT-Thread的操作系统特性,并解析...
然而,如果你不希望依赖于MFC库,或者想要一个更简洁的线程实现,可以创建自己的线程类,如题目中提到的`CThread`。这里我们将深入探讨`CThread`类的实现以及多线程编程的关键概念。 首先,`CThread`类通常会包含...
工程源码适合嵌入式软件初学者了解基于STM32和RT-Thread实现网络通讯、Modbus-TCP协议的方法,也包含了I2C总线、eeprom设备读写的相关操作,以及finsh组件函数添加方法。 资料中包含开发板原理图。
继承Thread类 1 实现Runable接口 2 继承Thread类: 3 必须重写run 方法:里面放置的实际的线程体 4 启动线程: 5 创建Thread对象 6 调用Thread对象的start 方法启动线程 7 在子类中定义static静态变量实现资源共享
这是实验一中thread 文档,通过8个测试,是timer总的.希望对大家有帮助。
“Libraries”目录可能包含了rt-thread所依赖的第三方库文件,这些库可能包含了数学运算、加密算法、图形界面等通用功能,以支持rt-thread实现更复杂的应用。 总的来说,rtthread_simulator_v0.1.0是一个集成了rt-...
1. **实时性**:RTThread 实现了抢占式调度,可以确保关键任务的及时响应。 2. **微内核设计**:基础内核小而精,只包含最基本的任务调度、内存管理和中断处理,提供高效运行环境。 3. **组件丰富**:包含TCP/IP网络...
### Python继承threading.Thread实现有返回值的子类实例 在Python中,多线程编程是一种常见的并发处理技术,能够有效提升程序的执行效率。`threading`模块是Python的标准库之一,它提供了创建和管理线程的功能。...
RT-Thread提供了丰富的中间件,如网络栈(TCP/IP协议栈)、文件系统、图形用户界面(GUI)、蓝牙栈、物联网协议栈等,这些中间件使得开发者可以轻松实现复杂的功能,如网络通信、本地存储和人机交互。 6. **开发...
RT-Thread实现的信号量支持多种操作,比如等待和信号操作,可用于解决多个线程之间的资源竞争问题,保证机器人系统中任务的有序执行。 3. 消息队列和邮箱:这些通信机制允许线程间或线程与内核之间的异步数据交换。...
通过以上步骤,开发者可以在NXP i.MX RT1052上有效地利用RT-Thread实现定时器功能,提高系统的实时性和效率。提供的资源代码应包含了具体的驱动实现和示例应用,便于理解和参考。在实际操作中,结合具体的项目需求和...