2013-08-19 22 views
5

Sono un principiante della filettatura. Non so esattamente quale sia la differenza tra i tre diversi tipi di modo in cui l'oggetto thread ha chiamato il metodo sleep. Inoltre si può chiarire in quale tipo di casi v'è una limitazione sull'utilizzo del modo in cui il metodo di sonno è stato chiamatoDiversi modi di chiamare il metodo sleep nei thread

Il codice è il seguente

// implementing thread by extending THREAD class// 

class Logic1 extends Thread 
{ 
    public void run() 
    { 
     for(int i=0;i<10;i++) 
     { 
      Thread s = Thread.currentThread(); 
      System.out.println("Child :"+i); 
      try{ 
       s.sleep(1000);    // these are the three types of way i called sleep method 
       Thread.sleep(1000);   //  
       this.sleep(1000);   // 
      } catch(Exception e){ 

      } 
     } 
    } 
} 

class ThreadDemo1 
{ 
    public static void main(String[] args) 
    { 
     Logic1 l1=new Logic1(); 
     l1.start(); 
    } 
} 
+2

'sleep()' è un metodo statico che fa sempre riferimento al thread di esecuzione corrente. –

+0

Non ci sono * tre * modi per chiamare il metodo 'sleep',' Thread # sleep' è 'static', quindi puoi chiamarlo direttamente dalla classe' Thread' o dall'attuale riferimento ad oggetti 'Thread'. –

+0

sono tutti uguali. : D – gumuruh

risposta

7

sleep() è un metodo statico che fa riferimento sempre la corrente thread in esecuzione.

Dal javadoc:

/** 
* Causes the currently executing thread to sleep (temporarily cease 
* execution) for the specified number of milliseconds, subject to 
* the precision and accuracy of system timers and schedulers. The thread 
* does not lose ownership of any monitors. 
* 
* @param millis 
*   the length of time to sleep in milliseconds 
* 
* @throws IllegalArgumentException 
*   if the value of {@code millis} is negative 
* 
* @throws InterruptedException 
*   if any thread has interrupted the current thread. The 
*   <i>interrupted status</i> of the current thread is 
*   cleared when this exception is thrown. 
*/ 
public static native void sleep(long millis) throws InterruptedException; 

Queste chiamate

s.sleep(1000); // even if s was a reference to another Thread 
Thread.sleep(1000);  
this.sleep(1000);  

sono tutti equivalenti a

Thread.sleep(1000); 
+0

Grazie, ma se implemento l'interfaccia eseguibile anziché estendere la classe thread e utilizzare direttamente sleep (1000); quindi sta dicendo che il metodo sleep non è stato trovato, perché è in esecuzione –

+3

Poiché l'interfaccia 'Runnable' non ha un metodo' sleep (long) '. 'Thread' ce l'ha. –

1

Tutti e tre sono uguali. Questi sono diversi modi per fare riferimento al thread attualmente in esecuzione.

2

In generale, se ClassName.method è un metodo statico di ClassName, e x è un'espressione il cui tipo è ClassName, quindi è possibile utilizzare x.method() e sarà lo stesso di chiamare ClassName.method(). Non importa quale sia il valore di x; il valore è scartato. Funzionerà anche se x è null.

String s = null; 
String t = s.format ("%08x", someInteger); // works fine 
              // (String.format is a static method) 
Problemi correlati