15.05.2013 Views

Clase Thread - docencia de la ETSIT-URJC

Clase Thread - docencia de la ETSIT-URJC

Clase Thread - docencia de la ETSIT-URJC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Otros<br />

Yield()<br />

• Avisa al scheduler que pue<strong>de</strong> interrumpir el thread. Es útil<br />

en algunos casos, pero no suele ser fiable.<br />

// Suggesting when to switch threads with yield().<br />

public c<strong>la</strong>ss Yielding<strong>Thread</strong> extends <strong>Thread</strong> {<br />

private static Test monitor = new Test();<br />

private int countDown = 5;<br />

private static int threadCount = 0;<br />

public Yielding<strong>Thread</strong>() {<br />

super("" + ++threadCount);<br />

start();<br />

}<br />

public String toString() {<br />

return "#" + getName() + ": " + countDown;<br />

}<br />

public void run() {<br />

while(true) {<br />

System.out.println(this);<br />

if(--countDown == 0) return;<br />

yield();<br />

}<br />

}<br />

public static void main(String[] args) {<br />

for(int i = 0; i < 5; i++)<br />

new Yielding<strong>Thread</strong>();<br />

}<br />

} //

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!