- 浏览: 71952 次
- 性别:
- 来自: 北京
文章分类
最新评论
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();
}
}
}
/**
* 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();
}
}
}
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 587well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 398http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 660this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 745http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 697http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 834What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 769http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 708I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 665http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 714http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1675http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 833Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 901The XML declaration typically a ... -
UCM
2010-05-08 11:41 745Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 763refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 843As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 936copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 817copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 866It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1309copy from http://blog.cornetdes ...
相关推荐
《吴恩达深度学习作业中的planar_utils.py与testCases_cv2.py解析》 在深度学习的学习旅程中,吴恩达的课程一直备受推崇,其课程作业中的代码往往能帮助我们深入理解理论知识。本文将重点解析两个关键文件——`...
吴恩达深度学习作业用到的planar_utils.py和testCases_cv2.py
《testCases.py与神经网络和深度学习编程实践》 在深度学习领域,特别是吴恩达教授的神经网络和深度学习课程中,动手实践是掌握知识的关键。`testCases.py`是一个重要的辅助工具,它为学员提供了验证代码正确性的...
《神经网络与深度学习实践资源:testCases_v2.zip》 在神经网络和深度学习的学习过程中,实践是不可或缺的一部分。"testCases_v2.zip" 是一个专门为Coursera上吴恩达教授的“神经网络与深度学习”课程设计的编程...
吴恩达 DeepLearning 1-3 testCases_v2.py 课后练习使用到的包
资源分类:Python库 所属语言:Python 资源全名:cfme_testcases-0.11.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
deeplearning.ai课程作业中部分使用的dnn_utils_v2,lr_utils,planar_utils,testCases,testCases_v2的代码,后续还会接着上传其他所需要使用的。
coursera的吴恩达的课编程练习所需的所需包和数据,可以方便学员自己在本地练习
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(Made for iPhone/iPod/iPad)认证测试用例的压缩文件,其中的主体文档是《MFi Suggested Test Cases.pdf》。MFi计划是苹果公司(Apple Inc.)为授权第三方制造商...
本文档 "How to Write Better Test Cases" 由Dianne L. Runnels撰写,旨在帮助读者提升测试用例的编写质量,从而提高整体的测试效率和软件质量。 1. **理解测试用例的基本概念** - 测试用例是一组详细的步骤,用于...
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.2.RELEASE.jar
spring-test-4.2.0.RELEASE.jar 集成了spring的mock包下的request和response方法,还有spring的test包下的应用方法
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. ...
"testCases_v2"和"planar_utils"是两个关键的组件,它们在学习过程中扮演着重要的角色。 首先,让我们深入了解一下"testCases_v2"。这是一个测试用例集合,用于验证和评估你的代码实现是否正确。在深度学习中,测试...
18800 SRVCC Test Cases V20.xlsx
spring-test-3.1.1.RELEASE.jar
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...