¿¹Á¦

public static abstract class StopSafeRunnable implements Runnable {
public final void run() {
while ( !stopped ) {
doRun();
}
}
public void stop() {
stopped = true;
}
public boolean isStopped() {
return stopped;
}
protected abstract void doRun();

private boolean stopped = false;
}

public static void main(String[] args) {
StopSafeRunnable runnable = new StopSafeRunnable() {
public void doRun() {
System.out.println( "Hello World" ); //$NON-NLS-1$
try { Thread.sleep( 1000 ); } catch (InterruptedException e) {}
}
};
Thread thread = new Thread( runnable );
thread.run();
try { Thread.sleep( 3000 ); } catch (InterruptedException e) {}
runnable.stop();
}

¼Ö·ç¼Ç
java.lang.Thread.run()À» È£ÃâÇÏÁö ¸¶½Ê½Ã¿À. ÀÌ ¸Þ¼Òµå È£ÃâÀº ½º·¹µå¸¦ ½ÃÀÛÇÏ´Â À߸øµÈ ¹æ½ÄÀ̸ç, ¿Ã¹Ù¸¥ ¹æ½ÄÀº java.lang.Thread.start()¸¸À» »ç¿ëÇØ¾ß ÇÕ´Ï´Ù.

public static abstract class StopSafeRunnable implements Runnable {
public final void run() {
while ( !stopped ) {
doRun();
}
}
public void stop() {
stopped = true;
}
public boolean isStopped() {
return stopped;
}
protected abstract void doRun();

private boolean stopped = false;
}

public static void main(String[] args) {
StopSafeRunnable runnable = new StopSafeRunnable() {
public void doRun() {
System.out.println( "Hello World" ); //$NON-NLS-1$
try { Thread.sleep( 1000 ); } catch (InterruptedException e) {}
}
};
Thread thread = new Thread( runnable );
thread.start();
try { Thread.sleep( 3000 ); } catch (InterruptedException e) {}
runnable.stop();
}