`

thread 源码

    博客分类:
  • j2se
阅读更多
线程的状态:NEW , RUNNABLE , BLOCKED,WARITING , TIMED_WARNING , TERMATED 
public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,
        
        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,
        
        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or 
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,
    
        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the 
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         * 
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.  
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call 
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on 
         * that object. A thread that has called <tt>Thread.join()</tt> 
         * is waiting for a specified thread to terminate.
         */
        WAITING,
        
        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of 
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> 
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

 

join 方法 同步,

    /**
     * Waits at most <code>millis</code> milliseconds for this thread to 
     * die. A timeout of <code>0</code> means to wait forever. 
     *
     * @param      millis   the time to wait in milliseconds.
     * @exception  InterruptedException if any thread has interrupted
     *             the current thread.  The <i>interrupted status</i> of the
     *             current thread is cleared when this exception is thrown.
     */
    public final synchronized void join(long millis) 
    throws InterruptedException {
	long base = System.currentTimeMillis();
	long now = 0;

	if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
	}

	if (millis == 0) {
	    while (isAlive()) {
		wait(0);
	    }
	} else {
	    while (isAlive()) {
		long delay = millis - now;
		if (delay <= 0) {
		    break;
		}
		wait(delay);
		now = System.currentTimeMillis() - base;
	    }
	}
    }

 Runnable接口:

public
interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used 
     * to create a thread, starting the thread causes the object's 
     * <code>run</code> method to be called in that separately executing 
     * thread. 
     * <p>
     * The general contract of the method <code>run</code> is that it may 
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

 

分享到:
评论

相关推荐

    rt-thread源码

    RT-Thread实时操作系统是一个分层的操作系统,它包括了: 底层移植、驱动层,这层与硬件密切相关,由Drivers和CPU移植相构成。 硬实时内核,这层是RT-Thread的核心,包括了内核系统中对象的实现,例如多线程及其...

    rt-thread物联网操作系统 v3.1.4-源码.zip

    《rt-thread物联网操作系统 v3.1.4-源码.zip》是针对rt-thread物联网操作系统的源码包...通过深入学习和实践rt-thread源码,开发者可以提升在物联网操作系统领域的专业技能,为智能硬件和物联网应用开发打下坚实基础。

    实时操作系统+rt-rtthread源码

    深入研究RT-Thread源码可以帮助开发者更好地理解RTOS的工作原理,从而更高效地利用其特性来开发高效、可靠的嵌入式系统。对于初学者,建议从基础概念入手,逐步学习并实践代码,而对于有经验的开发者,源码分析可以...

    rtthread源码

    关于rt-thread实时操作系统的源码,rtthread是一个来自中国的开源物联网操作系统,它具备非常强的可伸缩能力:从一个可以运行在 ARM Cortex-M0 芯片上的极小内核,到中等的 ARM Cortex-M3/4/7 系统,甚至是运行于 ...

    rt-thread内核源码

    《深入解析rt-thread内核源码》 rt-thread是一个开源、免费、实时性强、可裁剪的嵌入式操作系统内核,它具有轻量级、高效稳定的特点,被广泛应用于各种嵌入式设备中。rt-thread的操作系统内核源码是理解其工作原理...

    QEMU/mini2440上运行RT-Thread源码及工具

    1. 首先,解压`rt-thread-0.4.0 beta1`,这包含了RT-Thread的源码。 2. 安装Python 2.5.2,确保环境变量设置正确,可以执行Python脚本。 3. 安装SCONS,将其添加到PATH环境变量,以便在命令行中调用。 4. 使用SCONS...

    2-RT-Thread源码及官方参考资料.zip

    4. **rtthread nano 源码 3.0.3版本.rar**:这是RT-Thread的nano版本源码,适用于资源有限的嵌入式平台。RT-Thread nano是一个轻量级的分支,它保留了基本的实时性和多任务能力,但去除了许多非必要的组件,以实现更...

    rt-thread 3.0.3 源码压缩包

    rt-thread 3.0.3 源码压缩包,也可去gayhub上下载,或者官网。

    rt-thread 完整源代码

    RT-Thread 是一款主要由中国开源社区主导开发的开源实时操作系统。实时线程操作系统不仅仅是一个单一的实时操作系统内核,它也是一个完整的应用系统,包含了实时、嵌入式系统相关的各个组件:TCP/IP协议栈,文件系统...

    RT-thread源码包,移植源码包,开发板移植实时操作系统使用

    对应文章开发板移植RT-thread

    boost线程库源码,程序员自用

    Boost库本身是一个开源集合,包含了各种各样的高质量、跨平台的C++库,其中线程库(Boost.Thread)是提升C++多线程编程能力的重要组件。这个压缩包文件"boost_1_85_0"包含了Boost库的一个特定版本,即1.85.0版,这...

    STM32F+ RT-Thread工程源码实验

    2. **RT-Thread移植**:下载并集成RT-Thread源码到项目中,配置RTOS的参数,如任务数量、堆内存大小等。 3. **驱动开发**:根据需要编写或适配STM32的硬件驱动,如GPIO、串口、定时器等,使RT-Thread能有效控制硬件...

    ThreadX_源码.rar

    threadx 源码

    DreamCats#java-notes#Thread源码1

    // run的时候要用到// 线程组// 这里很重要,TL的变量在Thread里面自定义的一种Map方法public synchronized void sta

    RT-Thread源代码

    由于上传大小限制,这是第一部份。要把part1,2,3一起下载并解压。

    STM32F429阿波罗基于rtthread移植USBHID

    然后,下载RT-Thread源码并进行编译,确保开发环境(如MDK或GCC)已经正确设置。 2. **USB驱动编写**:在STM32F429中,USB功能由USB OTG FS(Full Speed)控制器提供。开发者需要根据官方参考手册编写USB初始化代码...

    java Thread

    两个售票窗口同时出售50张票; 程序分析:1.票数要使用同一个静态值 2.为保证不会出现卖出同一个票数,要java多线程同步锁。 设计思路:1.创建一个站台类Station,继承Thread,重写run...里面有源码,导入myeclipse执行

    ThreadX源码及文档

    ThreadX是一个实时操作系统(RTOS),在嵌入式系统开发...从理论到实践,从源码解析到平台移植,涵盖了ThreadX操作系统的方方面面。开发者可以借此深入了解RTOS的工作机制,提高自己在嵌入式系统设计和调试方面的能力。

Global site tag (gtag.js) - Google Analytics