線程就是 程序運行的一條線路。
1.新線程的創建
class Mythread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<20;i++){
System.out.println(123);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread th=new Thread(new ThreadTest().new Mythread());
th.start();//開啓線程
}
2.Thread 的方法的介紹
start()開啓線程
boolean isAlive();判斷線程是否活着
final String getName(); 獲得線程名
setName(String name) ; 設置線程名;
setPriority(int Priproty) 設置線程的優先級1-10; 數字越大優先級越高
sleep(long mills);線程休眠
interrupt();線程中斷
getState() 獲得線程的狀態
currentthread() 獲得當前線程的對象
set Daemon(boolean on );標記爲守護線程 或用戶線程
thread.join 加入線程
Thread.yield();線程讓步