您的位置:首页 > 其它

用线程终止while循环

2011-02-28 22:39 253 查看
public class StartTest {

	public static void main(String[] args) {
		Server s = new Server();
		s.start();
		s.end();
	}

}

public class Server {

	private Thread thread;
	
	public void start(){
		MyThread myThread = new MyThread();
		thread = new Thread(myThread);
		
		thread.setDaemon(true);
		thread.start();
	}
	
	public void end(){
		thread.interrupt();
	}
	
	private class MyThread implements Runnable{
		public void run(){
			int i = 0;
			
			while(true){
				System.out.println(i++);
			}
		}
		
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐