java.lang.Thread
【創(chuàng)建新線程的方法有兩種】
1 定義線程類實現(xiàn)Runnable接口
public class MyThred implements Runnable{
@overwrite
public void run(){}
}
起用線程:
MyThred myThred=new MyThred();
Thread t=new Thread(myThred);
t.start();
2 定義線程類繼承Thread類并重寫其run方法
public class MyThred extends Thread{
@overwrite
public void run(){}
}
起用線程:
MyThred myThred=new MyThred();
myThred.start();
說明:一般使用接口來實現(xiàn)
-------------------------------------------------
【線程狀態(tài)轉(zhuǎn)換】
【線程控制基本方法】:
isAlive() 判斷線程是否還“活”著
getPriority() 獲得線程的優(yōu)先級
setPriority() 設(shè)置線程的優(yōu)先級
Thread.sleep() 將當(dāng)前線程睡眠指定的毫秒數(shù)
join() 調(diào)用某線程的該方法,將當(dāng)前線程與該線程“合并”,即等待
該線程線束,再恢復(fù)當(dāng)前線程的運行。
yield() 讓出CPU,當(dāng)前線程進入就緒隊列等待調(diào)度
wait() 當(dāng)膠線程進入對象的wait pool。
notify()/notifyAll() 喚醒對象的wait pool中的一個/所有等待線程。
Thread.currentThread() 得到當(dāng)前線程
----------------------------------------------------------------
【sleep/join/yield 方法】
sleep示例:
public class TestInterrupt {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
//主線程睡眠
try {Thread.sleep(10000);}
catch (InterruptedException e) {}
//停下當(dāng)前線程,這個方法一般不建議使用
thread.interrupt();
//使用這個結(jié)束線程
shutDown();
}
}
class MyThread extends Thread {
boolean flag = true;
public void run(){
while(flag){
System.out.println("==="+new Date()+"===");
try {
//當(dāng)前子線程睡眠
sleep(1000);
//sleep時被打斷會拋InterruptedException
} catch (InterruptedException e) {
return;
}
}
}
//結(jié)束線程的方法
public void shutDown(){
flag = false;
}
}
jion示例:
public class TestJoin {
public static void main(String[] args) {
MyThread2 t1 = new MyThread2("abcde");
t1.start();
try {
//當(dāng)前線程合并到主線程,相當(dāng)于方法調(diào)用
t1.join();
} catch (InterruptedException e) {}
for(int i=1;i<=10;i++){
System.out.println("i am main thread");
}
}
}
class MyThread2 extends Thread {
//給當(dāng)前線程起個別名
MyThread2(String s){
super(s);
}
public void run(){
for(int i =1;i<=10;i++){
System.out.println("i am "+getName());
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}
yield示例:
public class TestYield {
public static void main(String[] args) {
//可以用同一個線程類創(chuàng)建不同的線程對象,分別執(zhí)行
MyThread3 t1 = new MyThread3("t1");
MyThread3 t2 = new MyThread3("t2");
t1.start(); t2.start();
}
}
class MyThread3 extends Thread {
MyThread3(String s){super(s);}
public void run(){
for(int i =1;i<=100;i++){
System.out.println(getName()+": "+i);
if(i%10==0){
//讓出時間給其它線程執(zhí)行
yield();
}
}
}
}
--------------------------------------------------------------
【線程的優(yōu)先級別】getPriority()/setProority(int newPriority)
線程的優(yōu)先級用數(shù)字表示,范圍從1到10,默認(rèn)為5.
Thread.MIN_PRIORITY=1
Thread.MAX_PRIORITY=10
Thread.NORM_PRIORITY=5
示例:
public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
//將t1的優(yōu)先級提高3級
t1.setPriority(Thread.NORM_PRIORITY + 3);
t1.start();
t2.start();
}
}
class T1 implements Runnable {
public void run() {
for(int i=0; i<1000; i++) {
System.out.println("T1: " + i);
}
}
}
class T2 implements Runnable {
public void run() {
for(int i=0; i<1000; i++) {
System.out.println("------T2: " + i);
}
}
}
--------------------------------------------------------------------
【線程的同步】未看完
posted on 2009-11-29 21:30
junly 閱讀(194)
評論(0) 編輯 收藏 所屬分類:
java