这里,事件线程负责修改组件内容和响应事件(如点击按纽)invokeLater是把RUN块放入事件线程队列中,而此线程马上返回,所以点击“好”后,事件线程先响应事件,再修改组件内容。而“坏”按纽则没有这样处理,所以“坏”按纽一直处于按下状态,待事件线程有空闲时才响应。点击两个按钮中的一个,没有使另一个的线程中断,只是事件线程太忙,无法同时照顾两个线程的“快速(隔1毫秒)”调用。将其改为1000就不会出现这些问题。

解决方案 »

  1.   

    这里的“响应事件”指按纽按下弹出状态的修改,另外,线程并没有中断,将sleep(1)改为sleep(1000)可以清楚地看到两个线程同时运行的情况。
      

  2.   

    to cs_tar:
      我很想看懂你说的invoke的问题,可还是没看懂,关键是几个主语你没说清楚,不知你指的线程、事件具体是什么含义,另外,点击一个按钮确实能够中止另一个,不然,两个线程不就同时修改组合框的东西了吗,我跟踪了一下,确实是中断了。
      

  3.   

    j2se的人气太差了。也许和难度成反比
      

  4.   

    修改SWING组件外观是事件线程做的,如果事件线程太忙,就很难同时进行多个SWING组件外观的修改,这里有修改按纽状态及修改JcomBox的内容。另外,线程确实没有中断,你将两个线程插入的数据改一下,一个插入10以内的数据,一个插入100以上的数据,再将睡眠时间改成1000,就可以清楚看到结果。
      

  5.   

    如果没有中断,if (!interrupted()) System.out.println("中断了");
    这句就不可能输出,但结果是输出了。
      

  6.   

    interrupted
    public static boolean interrupted()Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).
    Returns:
    true if the current thread has been interrupted; false otherwise.
    See Also: 
    isInterrupted()