2015-08-09 23 views
-2

請回答我的問題需要help.getting錯誤重新編譯與xlint。 Java使用或覆蓋棄用的API。這是第三次在嘗試發佈此問題,仍然無法發佈,如果這個問題得到張貼這一次,請回答我的問題編譯時出錯:threadsmethods.java使用或覆蓋棄用的API。使用xlint重新編譯

class a extends Thread { 

public void run() 

{ 

for(int i=1;i<=5;i++) 

{ 

if(i==1) 

{ 

yield(); 

} 

System.out.println("message from a "+i); 

} 

System.out.println("exit from a"); 

}} 

class b extends Thread 

{ 

public void run() 

{ 

for(int i=1;i<=5;i++) 

{ 

System.out.println("message from b "+i); 

if(i==3) 

stop(); 

} 

System.out.println("exit from b"); 

}} 

class c extends Thread 

{ 

public void run() 

{ 

for(int i=1;i<=5;i++) 

{ 

System.out.println("message from c" +i); 

if(i==1) 

try { 

sleep(1001); } 

catch(Exception e){ 

}} 

System.out.println("exit from c"); 

}} 

class threadsmethods 

{ 

public static void main(String args []) 

{ 

System.out.println("started Thread a"); 

new a().start(); 

System.out.println("Thread b started"); 

new b().start(); 

System.out.println("Thread c started"); 

new c().start(); 

System.out.println("end of main Thread"); 

}} 

回答

1

線程的stop方法已被棄用,因此你看到的警告。

如果你只是想在3次迭代後停止線程,那麼你應該從循環中斷開,並且你會從run方法中退出,這會自動停止你的線程。而不是使用停止使用類似:

if(i==3) 
    break; 

一旦循環將退出,這將使您的線程優雅地退出。

+0

謝謝你。但是如果我想要使用停止方法。我怎麼做?請回答 – user187744

+1

這只是一個警告,而不是一個錯誤,所以你仍然可以繼續前進。話雖如此,'stop'方法有一些問題,因此不推薦使用。 – SMA

+0

謝謝你。這解決了我的問題。如果你不介意回答。什麼是depreacated ?.在java中是一個初學者 – user187744