1.線程的優先級
每一個java線程都有一個優先級,這樣有助於操作系統確定線程的調度順序。
java線程的優先級是一個整數,其取值範圍是1-10。
默認情況下每個線程的優先級爲5。
具有較高優先級的線程對程序更重要,並且應該在低優先級的線程之前分配處理器資源。但是,線程優先級不能保證線程執行的順序,而且非常依賴於平臺。
public class Runabled implements Runnable { @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+":"+i); } } }@Test public void runnableTest(){ Thread t1=new Thread(new Runabled(),"thread1"); Thread t2=new Thread(new Runabled(),"thread2"); t1.setPriority(1); t2.setPriority(10); t1.start(); t2.start(); }
執行結果如下
thread2:0
thread2:1
thread2:2
thread2:3
thread2:4
thread2:5
thread2:6
thread2:7
thread2:8
thread1:0
thread2:9
thread1:1
thread1:2
thread1:3
thread1:4
thread1:5
thread1:6
thread1:7
thread1:8
thread1:9
2.Thread方法
序號 | 方法描述 |
---|---|
1 | public void start() 使該線程開始執行;Java 虛擬機調用該線程的 run 方法。 |
2 | public void run() 如果該線程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run 方法;否則,該方法不執行任何操作並返回。 |
3 | public final void setName(String name) 改變線程名稱,使之與參數 name 相同。 |
4 | public final void setPriority(int priority) 更改線程的優先級。 |
5 | public final void setDaemon(boolean on) 將該線程標記爲守護線程或用戶線程。 |
6 | public final void join(long millisec) 等待該線程終止的時間最長爲 millis 毫秒。 |
7 | public void interrupt() 中斷線程。 |
8 | public final boolean isAlive() 測試線程是否處於活動狀態。 |
測試線程是否處於活動狀態。 上述方法是被Thread對象調用的。下面的方法是Thread類的靜態方法。
序號 | 方法描述 |
---|---|
1 | public static void yield() 暫停當前正在執行的線程對象,並執行其他線程。 |
2 | public static void sleep(long millisec) 在指定的毫秒數內讓當前正在執行的線程休眠(暫停執行),此操作受到系統計時器和調度程序精度和準確性的影響。 |
3 | public static boolean holdsLock(Object x) 當且僅當當前線程在指定的對象上保持監視器鎖時,才返回 true。 |
4 | public static Thread currentThread() 返回對當前正在執行的線程對象的引用。 |
5 | public static void dumpStack() 將當前線程的堆棧跟蹤打印至標準錯誤流。 |
2.1sleep,join,yield方法介紹
sleep方法:讓線程休眠,Thread類的靜態方法。
public class Sleep {
public static void main(String args[])throws Exception{
MyThead myThead=new MyThead();
myThead.start();
myThead.sleep(10000);
myThead.flag=false;
}
}
class MyThead extends Thread{
boolean flag=true;
@Override
public void run() {
while (flag){
System.out.println("==================="+new Date().toLocaleString()+"===================");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 執行結果
===================2019-2-15 13:23:24===================
===================2019-2-15 13:23:25===================
===================2019-2-15 13:23:26===================
===================2019-2-15 13:23:27===================
===================2019-2-15 13:23:28===================
===================2019-2-15 13:23:29===================
===================2019-2-15 13:23:30===================
===================2019-2-15 13:23:31===================
===================2019-2-15 13:23:32===================
===================2019-2-15 13:23:33===================
join方法:調用join方法合併線程,使得線程之間的並行執行變成串行執行
public class Sleep {
public static void main(String args[])throws Exception{
MyThead myThead=new MyThead("A");
MyThead my=new MyThead("B");
myThead.start();
myThead.join(3000);//線程A先執行3秒,3秒後交替
my.start();
}
}
class MyThead extends Thread{
MyThead(String name) {
super(name);
}
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println("this is "+Thread.currentThread().getName());
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
}
}
執行結果
this is A
this is A
this is A
this is B
this is A
this is B
this is A
this is B
this is B
this is B
yield方法:使得當前線程的運行狀態變成就緒狀態。即將下一次執行的機會讓掉,讓其他或者自己的線程執行,也就是誰先搶到誰執行。
public class Sleep {
public static void main(String args[])throws Exception{
MyThead myThead=new MyThead("A");
MyThead my=new MyThead("B");
MyThead c=new MyThead("C");
myThead.start();
c.start();
my.start();
}
}
class MyThead extends Thread{
MyThead(String name) {
super(name);
}
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println("this is "+Thread.currentThread().getName());
if(i%3==0){
System.out.println("--------");
yield();
}
}
}
}
執行結果
this is A
--------
this is B
--------
this is C
--------
this is B
this is A
this is A
this is A
--------
this is B
this is C
this is A
this is C
this is B
--------
this is C
--------
this is B
this is C