1.獲取線程名、設置線程名
package com.fenqing.duoxiancheng;
public class d4_threadMethod {
public static void main(String[] args) {
/*
* 通過構造函數可以傳入String類型的名字
* 通過getName()方法獲取線程對象的名字
*/
new Thread("1---"){
public void run(){
System.out.println(this.getName()+"11111111111111111111");
}
}.start();
new Thread("2---"){
public void run(){
System.out.println(this.getName()+"2222222222222222222");
}
}.start();
/*
* 通過setName(String)方法可以設置線程對象的名字
*/
Thread t1=new Thread(){
public void run(){
System.out.println(this.getName()+"3333333333333333333333333");
}
};
t1.setName("3---"); //設置線程名
t1.start(); //最後不要忘記調用start()方法
//或者可以這樣寫:
new Thread(){
public void run(){
this.setName("4---");
System.out.println(this.getName()+"4444444444444444444444444");
}
}.start();;
}
}
2.抓獲正在執行的線程對象
package com.fenqing.duoxiancheng;
public class d5_currentThread {
public static void main(String[] args) {
new Thread(){
public void run(){
System.out.println("aaaaaaa");
}
}.start();
new Thread(new Runnable(){
@Override
public void run() {
//Thread.currentThread()獲取當前正在執行的線程(對象)
System.out.println(Thread.currentThread().getName()+"bbbbbbb");
}
}).start();
Thread.currentThread().setName("這是主線程哦");
System.out.println(Thread.currentThread().getName()); //獲取主線程的線程名
}
}
3.sleep休眠線程
package com.fenqing.duoxiancheng;
public class d6_sleepMothod {
/*
* sleep() 是靜態方法,可以直接調用,Thread.sleep()
* sleep(參數1,參數2) 參數1表示毫秒,參數2表示納秒,一般情況下不用納秒,只用一個參數。
* 程序執行時會等待參數1的時間間隔再繼續執行程序。
* 使用時,會出現中斷異常,拋出即可
*/
public static void main(String[] args) {
new Thread(){
public void run(){
for(int i=0;i<100;i++){
System.out.println("線程"+this.getName()+"執行了"+(i+1)+"次");
try {
Thread.sleep(1000); //1000毫秒=1秒
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}.start();
}
}
4.setDaemon()設置守護線程
設置一個線程爲守護線程, 該線程不會單獨執行, 當其他非守護線程都執行結束後, 自動也結束了
package com.fenqing.duoxiancheng;
public class d7_daemonMethod {
public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
for(int i=1;i<11;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName()+"執行了"+i+"次");
}
}
};
Thread t2=new Thread(){
public void run(){
for(int i=1;i<21;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName()+"執行了"+i+"次");
}
};
t2.setDaemon(true); //設置t2爲守護線程,當t1結束,t2也結束了,
//但是有時候會因爲有時間緩衝,所以當t1結束時,t2的某些語句也會出現在控制檯上
t1.start();
t2.start();
}
}
5.join加入線程
相當於插隊。
package com.fenqing.duoxiancheng;
public class d8_join {
/*
* join(), 當前線程暫停, 等待指定的線程執行結束後, 當前線程再繼續
* join(int), 可以等待指定的毫秒之後繼續
*/
public static void main(String[] args) {
final Thread t1=new Thread(){
public void run(){
for(int i=1;i<11;i++){
System.out.println(this.getName()+"執行了"+i+"次");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t2=new Thread(){
public void run(){
for(int i=1;i<21;i++){
if(i==6){
try {
//t1.join(); //不設置時間,就要等t1全部執行完
t1.join(50); //設置插隊的時間50ms,
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.getName()+"執行了");
}
}
};
t1.start();
t2.start();
}
}
6.禮讓線程
package com.fenqing.duoxiancheng;
public class d9_yield {
/*
* 讓出cpu讓別的線程做
*/
public static void main(String[] args) {
myThread m1=new myThread();
m1.start();
new myThread().start(); //定義兩個線程
}
}
class myThread extends Thread{
public void run(){
for(int i=0;i<100;i++){
if(i%10==0){
Thread.yield(); //當條件滿足時,禮讓
}
System.out.println(this.getName()+"——————"+(i+1)); //否則打印
}
}
}
禮讓線程結果: