?? exam10_9.java
字號:
/*這是一個多線程的測試程序,主要演示同步化方法處理
*程序的名字:Exam10_9.java
*/
public class Exam10_9 implements Runnable
{
/*******同步化方法out()******/
synchronized void out()
{
System.out.print("線程名="+Thread.currentThread().getName());
System.out.println(" 優先級="+Thread.currentThread().getPriority());
}
/****實現Runnable接口方法****/
public void run()
{
for(int i=1;i<=3;i++) //循環3次結束
{
try
{
Thread.sleep(9); //休眠千分之五毫秒
}
catch (InterruptedException e) //捕捉中斷異常
{
System.out.println(e.toString());
}
out();
}
}
/*****主方法main()*****/
public static void main(String[] args)
{
Exam10_9 exam=new Exam10_9();
Thread thread;
for(int i=1; i<=3;i++) //循環3次創建3個線程
{
thread=new Thread(exam); //以本類對象為參數創建線程
thread.setPriority((int)(10*Math.random()+1)); //設置隨機優先級
thread.start(); //啟動線程
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -