자바스레드 샘플코드
class ThreadTest implements Runnable{ private int loopCount; private String code_name; public ThreadTest(String code_name){ this.code_name = code_name; } public void run(){ loopCount = 0; try { while ( !Thread.currentThread().isInterrupted() && //외부 종료용 this.loopCount < 10) { //내부 종료 용 System.out.println("Thread is alive.." + loopCount + this.code_name); // 0.5초간 멈춤. Thread.sleep(500); loopCount++; } } catch ( Exception ex ) { System.out.println( ex ); } finally { System.out.println("end Thread........"); } } public int getLoopCount() { return loopCount; } public void setLoopCount(int loopCount) { this.loopCount = loopCount; } } public class ThreadExample { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub for (int idx=0; idx<3; idx++) { Thread thread = new Thread( new ThreadTest( "codename " + Integer.toString(idx) ) ); thread.start(); //멈추고 싶을때 trdTest.interrupt(); } try { Thread.sleep(2000); } catch ( Exception ex ) {} } }
출처 : http://code.p-ark.co.kr/143