日期:2014-05-20 浏览次数:20880 次
Collection 类 本身 extends Threads 代码太多,只贴上关键代码 private Object lock = new Object(); private Object task = new Object(); public String getSuspend() { return suspend; } public void setSuspend(String suspend) { this.suspend = suspend; } private void Execute(HashMap map) { ................. long a = System.currentTimeMillis(); for (int i = 0; i < list.size(); i++) { synchronized (task) { // 我现在有一个SWING界面 有4个按钮,暂停按钮 setSuspend("1") if (getSuspend().equals("1")) { try { this.Notify("a"); // 当暂停按钮别按下否,开始循环检查 新的 getSuspend() task.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } ..............执行任务 } } private void Resume() { synchronized (lock) { this.setSuspend("0"); lock.notifyAll(); // 这里为什么不能把暂停的线程给唤醒呢? Thread.yield(); } } private void Stop() { this.interrupt(); this.Close(); return; } private void Notify(String name) { Thread t = new Thread(name) { public void run() { boolean f = true; while (f) { try { if (getSuspend().equals("2")) { f = false; Resume(); } else if (getSuspend().equals("3")) { f = false; Stop(); } Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } }; t.start(); }