測試Thread類中的常用方法

2021-10-11 14:39:03 字數 1067 閱讀 2645

/*

* 測試thread類中的常用方法:

* 1、start():啟動當前執行緒;呼叫當前執行緒的run()方法

* 2、run():通常重寫thread類中的此方法,將建立的執行緒要執行的操作宣告在此方法中

* 3、currentthread():靜態方法,返回執行當前**的執行緒

* 4、getname():獲取當前執行緒的名字

* 5、setname():設定當前執行緒的名字

* 6、yield():當執行緒執行此方法的時候,釋放當前的cpu的執行權

* 8、stop():當執行此方法時,強制結束當前執行緒;(已過時)

* 9、sleep(long millitime):讓當前執行緒「睡眠」指定的millitime毫秒,在指定的millitime毫秒之內,當前執行緒是

* 阻塞狀態。

* 10、isalive():判斷當前執行緒是否存活。

* */

class

hellothread

extends

thread

catch

(interruptedexception e)

system.out.

println

(thread.

currentthread()

.getname()

+":"

+i);

}// if (i % 20==0)}}

//提供帶引數的構造器(在建立物件的時候就給屬性進行初始化)

public

hellothread

(string name)

}public

class

threadmethodtest

if(i ==20)

catch

(interruptedexception e)}}

system.out.

println

("當前執行緒是否存活:"

+h1.

isalive()

);}}

測試Thread中的常用方法

package com.day0205 2 測試thread中的常用方法 1.start 啟動當前執行緒 呼叫當前執行緒的run 2.run 通常需要重寫thread類的方法,將建立的執行緒要執行的操作宣告在此方法中 3.currentthread 靜態方法,返回當前 的執行緒 4.getname ...

Thread類中的常用方法

1.start 啟動當前執行緒 呼叫當前執行緒的run 2.run 通常需要重寫thread類中的此方法,將建立的執行緒要執行的操作宣告在此方法中 3.currentthread 靜態方法,返回執行當前 的執行緒 4.getname 獲取當前執行緒的名字 5.setname 設定當前執行緒的名字 6...

Thread類中的常用的方法

執行緒的優先順序 1.max priority 10 min priority 1 norm priority 5 預設優先順序 2.如何獲取和設定當前執行緒的優先順序 getpriority 獲取執行緒的優先順序 setpriority int p 設定執行緒的優先順序 說明 高優先順序的執行緒要...