?? exam10_6.java
字號:
/*這是一個同級別3個線程的演示示例
*程序的名稱:ThreadExam10_6.java
*/
public class Exam10_6 extends Thread
{
static int n1=0,n2=0,n3=0;
public Exam10_6(String strName)
{
this.setName(strName); //命名線程
this.setDaemon(true); //設置線程為守護線程
}
public void run()
{
while(true)
{
if(n3>=3) System.exit(0); //執行3次后結束運行
String name=Thread.currentThread().getName(); //獲取線程名
if(name.equals("a"))
{
n1++;
System.out.println("線程a第"+n1+"次執行");
}
else if(name.equals("b"))
{
n2++;
System.out.println(" 線程b第"+n2+"次執行");
}
else if(name.equals("c"))
{
n3++;
System.out.println(" 線程c第"+n3+"次執行");
}
}
}
public static void main(String [] args)
{
Exam10_6 thread_a = new Exam10_6("a"); //創建線程對象a
Exam10_6 thread_b = new Exam10_6("b"); //創建線程對象b
Exam10_6 thread_c = new Exam10_6("c"); //創建線程對象c
thread_b.setPriority(10);
thread_c.setPriority(8);
thread_a.start(); //啟動線程a
thread_b.start(); //啟動線程b
thread_c.start(); //啟動線程c
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -