`
niwtsew
  • 浏览: 71952 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

TestCases to understand Thread.State

阅读更多
Explainations of Thread.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 Thread#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 Thread#sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link Thread#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;
    }



TestCases to understand Thread.State:
import junit.framework.TestCase;
import junit.framework.Assert;

public class ThreadStateTest extends TestCase {
    private Thread thread;
   
    public void setUp()throws Exception
    {
        super.setUp();
        thread = new Thread();
    }
    public void testThreadIsInNewStateAfterConstructedBeforeStarted()
    {
        Assert.assertEquals(Thread.State.NEW, thread.getState());

        thread.start();
        Assert.assertTrue("Thread state is not NEW after call of start()",Thread.State.NEW != thread.getState());
    }
   
    public void testThreadIsInRunnableStateAfterStarted()
    {
        thread.start();
        Assert.assertEquals(Thread.State.RUNNABLE, thread.getState());
    }
   
    public void testThreadIsInWaitingState(){
        thread = new Thread()
        {
            private Object lock = new Object();
            public void run()
            {
                synchronized(lock)
                {
                    try{
                        System.out.println("Getting into lock.wait()");
                        lock.wait();
                    }catch(InterruptedException interruptedEx)
                    {
                        interruptedEx.printStackTrace();
                    }
                }
            }
        };
        thread.start();
       
        //here hold a sec,make sure this thread gets the monitor.
        holdASec();
        Assert.assertEquals(Thread.State.WAITING,thread.getState());
    }
   
    public void testThreadIsInTimedWaitingState(){
        thread = new Thread()
        {
            private Object lock = new Object();
            public void run()
            {
                synchronized(lock)
                {
                    try{
                        System.out.println("Getting into lock.wait(time)");
                        lock.wait(50000);
                    }catch(InterruptedException interruptedEx)
                    {
                        interruptedEx.printStackTrace();
                    }
                }
            }
        };
        thread.start();
       
        //here hold a sec,make sure this thread gets the monitor.
        holdASec();
        Assert.assertEquals(Thread.State.TIMED_WAITING,thread.getState());
    }
   
    public void testThreadIsInBlockStateWhenWaitingToGetMonitor()
    {
        final Object sharedLock = new Object();
       
        Thread anotherThreadHoldsTheLockForAWhile = new Thread()
        {
            public void run()
            {
                synchronized(sharedLock)
                {
                    System.out.println(getName() + "holding sharedLock");
                   
                    try{
                        Thread.sleep(50000);
                    }catch(InterruptedException e)
                    {
                        e.printStackTrace();                           
                    }
                }
            }
        };
        anotherThreadHoldsTheLockForAWhile.start();
       
        //holds a sec to ensure the thread anotherThreadHoldsTheLockForAWhile got the sharedLock first
        holdASec();
        thread = new Thread()
        {
            public void run()
            {
                synchronized(sharedLock)
                {
                    System.out.println("Got the lock");
                }
            }
        };
        thread.start();
        holdASec();
        Assert.assertEquals(Thread.State.BLOCKED, thread.getState());
    }
   
    public void testThreadIsInTerminatedStateAfterCompletion()
    {
        thread.start();
        holdASec();
        Assert.assertEquals(Thread.State.TERMINATED, thread.getState());
    }
   
    private void holdASec()
    {
        try{
            Thread.sleep(5000);
        }catch(InterruptedException e)
        {
            e.printStackTrace();
        }   
    }
}


0
0
分享到:
评论

相关推荐

    吴恩达深度学习作业用到的planar_utils.py和testCases_cv2.py

    《吴恩达深度学习作业中的planar_utils.py与testCases_cv2.py解析》 在深度学习的学习旅程中,吴恩达的课程一直备受推崇,其课程作业中的代码往往能帮助我们深入理解理论知识。本文将重点解析两个关键文件——`...

    planar_utils.py和testCases_cv2.py

    吴恩达深度学习作业用到的planar_utils.py和testCases_cv2.py

    testCases.py

    《testCases.py与神经网络和深度学习编程实践》 在深度学习领域,特别是吴恩达教授的神经网络和深度学习课程中,动手实践是掌握知识的关键。`testCases.py`是一个重要的辅助工具,它为学员提供了验证代码正确性的...

    testCases_v2.zip

    《神经网络与深度学习实践资源:testCases_v2.zip》 在神经网络和深度学习的学习过程中,实践是不可或缺的一部分。"testCases_v2.zip" 是一个专门为Coursera上吴恩达教授的“神经网络与深度学习”课程设计的编程...

    吴恩达 DeepLearning 1-3 testCases_v2.py

    吴恩达 DeepLearning 1-3 testCases_v2.py 课后练习使用到的包

    Python库 | cfme_testcases-0.11.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:cfme_testcases-0.11.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    dnn_utils_v2,lr_utils,planar_utils,testCases,testCases_v2.zip

    deeplearning.ai课程作业中部分使用的dnn_utils_v2,lr_utils,planar_utils,testCases,testCases_v2的代码,后续还会接着上传其他所需要使用的。

    testCases_v3.py

    coursera的吴恩达的课编程练习所需的所需包和数据,可以方便学员自己在本地练习

    Springer.The.Developer’s.Guide.to.Debugging.2008.pdf

    7.3.1 How to Determine What the Current Thread is Executing . 94 7.3.2 Analyzing the Threads of the Program . . . . . 95 7.4 Familiarize Yourself with Threading Analysis Tools . . . . . 96 7.5 ...

    MFi Suggested Test Cases.zip

    《MFi Suggested Test Cases.zip》是一个包含MFi(Made for iPhone/iPod/iPad)认证测试用例的压缩文件,其中的主体文档是《MFi Suggested Test Cases.pdf》。MFi计划是苹果公司(Apple Inc.)为授权第三方制造商...

    How to Write Better Test Cases

    本文档 "How to Write Better Test Cases" 由Dianne L. Runnels撰写,旨在帮助读者提升测试用例的编写质量,从而提高整体的测试效率和软件质量。 1. **理解测试用例的基本概念** - 测试用例是一组详细的步骤,用于...

    Software Testing and Continuous Quality Improvement

    16 Steps to Develop and Implement a Software Quality Assurance Plan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Step 1. Document the Plan. . . ...

    spring-test-4.0.0.RELEASE.jar

    . spring-test-4.0.0.RELEASE.jar

    spring-test-4.0.2.RELEASE.jar

    spring-test-4.0.2.RELEASE.jar

    spring-test-4.2.0.RELEASE.jar

    spring-test-4.2.0.RELEASE.jar 集成了spring的mock包下的request和response方法,还有spring的test包下的应用方法

    Addison.Wesley.Test-Driven.iOS.Development

    How to Write a Unit Test Chapter 4. Tools for Testing Chapter 5. Test-Driven Development of an iOS App Chapter 6. The Data Model Chapter 7. Application Logic Chapter 8. Networking Code Chapter 9. ...

    吴恩达深度学习编程作业(1-3)testCases_v2和planar_utils包

    "testCases_v2"和"planar_utils"是两个关键的组件,它们在学习过程中扮演着重要的角色。 首先,让我们深入了解一下"testCases_v2"。这是一个测试用例集合,用于验证和评估你的代码实现是否正确。在深度学习中,测试...

    18800 SRVCC Test Cases V20.xlsx

    18800 SRVCC Test Cases V20.xlsx

    spring-test-3.1.1.RELEASE.jar

    spring-test-3.1.1.RELEASE.jar

    s3c2410测试开发程序

    2410init.o(Init) refers to 2410test.o(.text) for Main 2410test.o(.text) refers to 2410lib.o(.text) for Uart_Printf 2410test.o(.text) refers to mmu.o(.text) for MMU_Init 2410test.o(.text) refers to...

Global site tag (gtag.js) - Google Analytics