class a
{
public void u()
{
System.out.println("去上學");
}
/*public void t()
{
System.out.println("放學回家");
}*/
}
class b implements Runnable
{
public static boolean o=true;
public synchronized void run()
{
if(!o)
{
try
{
wait();
}
catch(Exception m){}
}
System.out.println("上課鐘響");
o=false;
notify();
}
}
class c implements Runnable
{
public void run()
{
for(int r=1;r {
System.out.println("這是第"+r+"堂課");
System.out.println("下課了");
}
System.out.println("放學回家");
}
}
public class te08
{
public static void main(String[] args)
{
a a1=new a();
//b b1=new b();
//c c1=new c();
a1.u();
Thread b2=new Thread(new b());
b2.start();
Thread c2 =new Thread(new c());
c2.start();
}
}
執行結果
--------------------Configuration:
去上學
上課鐘響
這是第1堂課
下課了
這是第2堂課
下課了
這是第3堂課
下課了
這是第4堂課
下課了
這是第5堂課
下課了
這是第6堂課
下課了
這是第7堂課
下課了
這是第8堂課
下課了
這是第9堂課
下課了
這是第10堂課
下課了
放學回家
Process completed. 編譯成功
- Jan 27 Sun 2013 21:13
java範例程式08 多執行緒與迴圈和資源同步
close
全站熱搜
留言列表
發表留言