博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thread State
阅读量:5213 次
发布时间:2019-06-14

本文共 2586 字,大约阅读时间需要 8 分钟。

 

public 
class ThreadStateTest {
    
public 
static 
void main(String[] args) {
        
//
 surprising
        
//
 Compile only iconst_1
        System.out.println(("hello123" == ("hel" + "lo" + 123)));
        Thread t = 
new Thread() {
            
public 
void run() {
                System.out.println("X: " + Thread.currentThread().getState());
            }
        };
        System.out.println(t.getState());
        t.start();
        t = 
new Thread() {
            
private Object lock = 
new Object();
            
public 
void run() {
                
synchronized (lock) {
                    
try {
                        System.out.println("Getting into lock.wait()");
                        lock.wait(2000);
                    } 
catch (InterruptedException interruptedEx) {
                        interruptedEx.printStackTrace();
                    }
                }
            }
        };
        t.start();
        
try {
            Thread.sleep(1000);
        } 
catch (InterruptedException e) {
        }
        System.out.println(t.getState());
        
try {
            Thread.sleep(3000);
        } 
catch (InterruptedException e) {
        }
        System.out.println(t.getState());
        
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();
        holdASec();
        System.out.println("XX: " + anotherThreadHoldsTheLockForAWhile.getState());
        t = 
new Thread() {
            
public 
void run() {
                
synchronized (sharedLock) {
                    System.out.println("Got the lock");
                }
            }
        };
        t.start();
        holdASec();
        System.out.println("YY: " + t.getState());
    }
    
private 
static 
void holdASec() {
        
try {
            Thread.sleep(5000);
        } 
catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

如题,上面是我按照下面网址参考写的测试代码,仅仅是测试。。。。

-------------------------------------------------------------------------------

 

Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.

Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.

 

-------------------------------------------------------------------------------

转载于:https://www.cnblogs.com/diyunpeng/archive/2012/04/13/2446382.html

你可能感兴趣的文章
excel表格数据转oracle可查数据
查看>>
智能玩具API文档
查看>>
【LeetCode & 剑指offer刷题】链表题10:328 Odd Even Linked List
查看>>
kindeditor官网异步加载示例无效,解决无法通过方法初始化编辑器
查看>>
HTTP POST GET 本质区别详解
查看>>
OC:继承、初始化方法、便利构造器
查看>>
Django---Model操作
查看>>
单元测试
查看>>
LeetCode 13: Roman to Integer
查看>>
数据库优化
查看>>
ZOJ 3659Conquer a New Region解题报告
查看>>
汇编指令: LGDT、LIDT、LLDT、LMSW、LOADALL、LOADALL286、LOCK、LODSB、LODSW、LODSD
查看>>
【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) B】Reach Median
查看>>
ActiveMQ的使用.Net,Python(一)
查看>>
hdu2059 dp
查看>>
POJ2985 并查集+线段树 求第k大的数
查看>>
后仿真学习总结
查看>>
Diversified Development of Crusher and Mill
查看>>
spring boot web相关配置
查看>>
BeanUtil 对象转json
查看>>