close

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<=8;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.  編譯成功 

 
arrow
arrow
    全站熱搜

    gtea0609 發表在 痞客邦 留言(0) 人氣()