下面列出了java.lang.Thread.State#TERMINATED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** taken from sun.misc.VM
*
* Returns Thread.State for the given threadStatus
*/
private static Thread.State toThreadState(int threadStatus) {
if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
return State.RUNNABLE;
} else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
return State.BLOCKED;
} else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
return State.WAITING;
} else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
return State.TIMED_WAITING;
} else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
return State.TERMINATED;
} else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
return State.NEW;
} else {
return State.RUNNABLE;
}
}
/** taken from sun.misc.VM
*
* Returns Thread.State for the given threadStatus
*/
private static Thread.State toThreadState(int threadStatus) {
if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) {
return State.RUNNABLE;
} else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {
return State.BLOCKED;
} else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) {
return State.WAITING;
} else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) {
return State.TIMED_WAITING;
} else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) {
return State.TERMINATED;
} else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) {
return State.NEW;
} else {
return State.RUNNABLE;
}
}
public static List<Thread> currentThreads() {
List<Thread> threads = Lists.newArrayList();
for (Thread thread : Thread.getAllStackTraces().keySet()) {
// DestroyJavaVM is a JVM thread that appears sporadically, easier to just filter it out
//
// AWT-AppKit is a JVM thread on OS X that appears during webdriver tests
//
// "process reaper" are JVM threads on linux that monitors subprocesses, these use a
// thread pool in jdk7 and so the threads stay around in the pool even after the
// subprocess ends, they show up here when mixing local container and javaagent
// container tests since javaagent container tests create subprocesses and then local
// container tests check for rogue threads and find these
if (thread.getState() != State.TERMINATED
&& !thread.getName().equals("DestroyJavaVM")
&& !thread.getName().equals("AWT-AppKit")
&& !thread.getName().equals("process reaper")) {
threads.add(thread);
}
}
return threads;
}
/** Updates the view in a new thread and shows loading indicator. */
public final void updateView() {
synchronized (updateLock) {
if ((updateThread == null || updateThread.getState() == State.TERMINATED)
&& initializationThread.getState() == State.TERMINATED) {
updateThread = getUpdateThread();
loadingView.showView();
updateThread.start();
} else {
rerunUpdate = true;
}
}
}
/**
* Returns whether the {@link AndroidDebugBridge} object is still connected to the adb daemon.
*/
public boolean isConnected() {
MonitorThread monitorThread = MonitorThread.getInstance();
if (mDeviceMonitor != null && monitorThread != null) {
return mDeviceMonitor.isMonitoring() && monitorThread.getState() != State.TERMINATED;
}
return false;
}
protected boolean isSaturated() {
if (getPoolSize() <= 3) {
return DEBUG;
}
int corePoolSize = getCorePoolSize();
int i = CountedTask.mNumRunning.get();
int size = mThreads.size();
if (i < corePoolSize || i < size) {
return true;
}
boolean z;
synchronized (mThreads) {
Iterator<Thread> it = mThreads.iterator();
size = 0;
while (it.hasNext()) {
State state = it.next().getState();
if (state == State.RUNNABLE || state == State.NEW) {
i = size + 1;
} else {
if (state == State.TERMINATED) {
it.remove();
}
i = size;
}
size = i;
}
}
z = size >= corePoolSize;
return z;
}
public void scanMediaItems() {
if (mLoadingThread == null || mLoadingThread.getState() == State.TERMINATED) {
isStopping = false;
Util.actionScanStart();
mLoadingThread = new Thread(new GetMediaItemsRunnable());
mLoadingThread.start();
}
}
public boolean isWorking() {
if (mLoadingThread != null &&
mLoadingThread.isAlive() &&
mLoadingThread.getState() != State.TERMINATED &&
mLoadingThread.getState() != State.NEW)
return true;
return false;
}
public void start(IVideoBrowser videoBrowser) {
isStopping = false;
if (mThread == null || mThread.getState() == State.TERMINATED) {
mVideoBrowser = new WeakReference<IVideoBrowser>(videoBrowser);
mThread = new Thread(this);
mThread.start();
}
}
/**
* Tests the improvement added by using a SecureRandom pool when generating GUID's
*/
public void testGuid()
{
// warm-up (to pre-init the secureRandomArray)
GUID.generate();
List<Thread> threads = new ArrayList<>();
int n = 30;
for (int i = 0; i < n; i++)
{
Thread thread = new Thread(new GuidRunner());
threads.add(thread);
thread.start();
}
Set<String> blocked = new HashSet<String>();
Set<String> terminated = new HashSet<String>();
int maxItemsBlocked = 0;
while (terminated.size() != n)
{
for (Thread current : threads)
{
State state = current.getState();
String name = current.getName();
if (state == State.BLOCKED)
{
if (!blocked.contains(name))
{
blocked.add(name);
maxItemsBlocked = blocked.size() > maxItemsBlocked ? blocked.size() : maxItemsBlocked;
}
}
else // not BLOCKED, eg. RUNNABLE, TERMINATED, ...
{
blocked.remove(name);
if (state == State.TERMINATED && !terminated.contains(name))
{
terminated.add(name);
}
}
}
}
//worst case scenario : max number of threads blocked at a moment = number of threads - 2 ( usually ~5 for 30 threads)
//the implementation without RandomSecure pool reaches constantly (number of threads - 1) max blocked threads
Assert.assertTrue("Exceeded number of blocked threads : " + maxItemsBlocked, maxItemsBlocked < n-2);
}
/**
* 开启打怪线程
* 自动检索线程是否开启,若没开启,则全部重新开启。若开启后没有执行完毕,则继续使用
*/
public void startDaGuaiThread(){
Command.mainThread=true; //线程运行状态开启
try {
if(Command.logThread.getState()==State.TERMINATED){
Command.logThread=null;
Command.logThread=new LogThread();
Command.logThread.start();
new Func.File().log("日志线程启动");
}
if(Command.daGuaiThread.getState()==State.TERMINATED){
Command.daGuaiThread=null;
Command.daGuaiThread=new DaGuaiThread();
Command.daGuaiThread.start();
new Func.File().log("打怪主线程启动");
}
if(Command.systemThread.getState()==State.TERMINATED){
Command.systemThread=null;
Command.systemThread=new SystemThread();
Command.systemThread.start();
new Func.File().log("系统线程启动");
}
} catch (Exception e) { //捕获异常,可能是线程未初始化,进行初始化线程并开启
if(e.getMessage()!=null){
new Func.File().log("线程初始化开启");
}
Command.daGuaiThread=new DaGuaiThread();
Command.logThread=new LogThread();
Command.systemThread=new SystemThread();
Command.daGuaiThread.start();
new Func.File().log("打怪主线程启动");
Command.logThread.start();
new Func.File().log("日志线程启动");
Command.systemThread.start();
new Func.File().log("系统线程启动");
}
Command.daGuaiThread.setPriority(1); //打怪主线程优先级最低
Command.logThread.setPriority(1); //日志线程,最低
Command.systemThread.setPriority(8); //系统线程最高
Command.daGuaiThread.setName("打怪主线程");
Command.logThread.setName("日志线程");
Command.systemThread.setName("系统线程");
}
/**
* 定点开鼓
*/
public void startSettledOpenGuThread(){
Command.mainThread=true; //线程运行状态开启
try {
if(Command.logThread.getState()==State.TERMINATED){
Command.logThread=null;
Command.logThread=new LogThread();
Command.logThread.start();
new Func.File().log("日志线程启动");
}
if(Command.settledOpenThread.getState()==State.TERMINATED){
Command.settledOpenThread=null;
Command.settledOpenThread=new SettledOpenThread();
Command.settledOpenThread.start();
new Func.File().log("定点扫描鼓主线程启动");
}
if(Command.systemThread.getState()==State.TERMINATED){
Command.systemThread=null;
Command.systemThread=new SystemThread();
Command.systemThread.start();
new Func.File().log("系统线程启动");
}
} catch (Exception e) { //捕获异常,可能是线程未初始化,进行初始化线程并开启
if(e.getMessage()!=null){
new Func.File().log("线程初始化开启");
}
Command.settledOpenThread=new SettledOpenThread();
Command.logThread=new LogThread();
Command.systemThread=new SystemThread();
Command.settledOpenThread.start();
new Func.File().log("打怪主线程启动");
Command.logThread.start();
new Func.File().log("日志线程启动");
Command.systemThread.start();
new Func.File().log("系统线程启动");
}
Command.settledOpenThread.setPriority(1); //打怪主线程优先级最低
Command.logThread.setPriority(1); //日志线程,最低
Command.systemThread.setPriority(8); //系统线程最高
Command.settledOpenThread.setName("定点开鼓主线程");
Command.logThread.setName("日志线程");
Command.systemThread.setName("系统线程");
}
/**
* 自动扫货
*/
public void startSaoHuoThread(){
Command.mainThread=true;
try{
if(Command.saoHuoThread.getState()==State.TERMINATED){
Command.saoHuoThread=null;
Command.saoHuoThread=new SaoHuoThread();
Command.saoHuoThread.start();
new Func.File().log("扫货主线程启动");
}
if(Command.logThread.getState()==State.TERMINATED){
Command.logThread=null;
Command.logThread=new LogThread();
Command.logThread.start();
new Func.File().log("日志线程启动");
}
if(Command.systemThread.getState()==State.TERMINATED){
Command.systemThread=null;
Command.systemThread=new SystemThread();
Command.systemThread.start();
new Func.File().log("系统线程启动");
}
}catch (Exception e) {
if(e.getMessage()!=null){
new Func.File().log("扫货线程开启异常捕获:"+e.getMessage());
}
Command.saoHuoThread=new SaoHuoThread();
Command.saoHuoThread.start();
new Func.File().log("扫货主线程启动");
Command.logThread=new LogThread();
Command.logThread.start();
new Func.File().log("日志线程启动");
Command.systemThread=new SystemThread();
Command.systemThread.start();
new Func.File().log("系统线程启动");
}
Command.saoHuoThread.setName("扫货主线程");
Command.saoHuoThread.setPriority(7); //监控优先级高!
Command.logThread.setName("日志线程");
Command.logThread.setPriority(1); //日志线程最低
Command.systemThread.setName("系统线程");
Command.systemThread.setPriority(8); //系统线程优先级最高
}