日期:2014-05-20 浏览次数:20792 次
class ChildThread implements Runnable { public static boolean stop=false; static int count = 0; public ChildThread() { } public void run() { while(!stop) { // 查找 search(); count++; } } // 查找 private void search() { System.out.println("子线程-查找"); } } public class xingfuweiyu implements Runnable { static ChildThread c=null; public static void main(String[] args) { // 调用主线程 xingfuweiyu m=new xingfuweiyu(); new Thread(m).start(); } public void run() { // 做些事 System.out.println("主线程"); // 调用子线程 c=new ChildThread(); new Thread(c).start(); try { Thread.sleep(3000); } catch (Exception e) { } ChildThread.stop = true; System.out.println("子线程-停止了,执行"+c.count+"次"); } }
------解决方案--------------------
setDaemon(true)
------解决方案--------------------