From Documentation

Jump to: navigation, search




It is possible to implement a long operation without suspend and resume. It is useful if the synchronization codes are going too complex to debug.

The idea is simple. The working thread save the result in a temporary space, and then the onTimer event listener pops the result to the desktop.

 //WorkingThread2
 package test;

import java.util.List;

import org.zkoss.zk.ui.Desktop;
import org.zkoss.zul.Label;
 
 
 public class WorkingThread2 extends Thread {
     private static int _cnt;
     private final Desktop _desktop;
     private final List _result;
 
     public WorkingThread2(Desktop desktop, List result) {
         _desktop = desktop;
         _result = result;
     }
     public void run() {
         _result.add(new Label("Execute "+ ++_cnt));
     }
 }

Then, you append the labels in the onTimer event listener.

<window id="main" title="Working Thread2">
    <zscript>
         int numPending = 0;
         List result = Collections.synchronizedList(new LinkedList());
    </zscript>
    <button label="Start Working Thread">
        <attribute name="onClick">
             ++numPending;
             timer.start();
             new test.WorkingThread2(desktop, result).start();
        </attribute>
    </button>
    <timer id="timer" running="false" delay="1000" repeats="true">
        <attribute name="onTimer">
             while (!result.isEmpty()) {
             main.appendChild(result.remove(0));
             --numPending;
             }
             if (numPending == 0) timer.stop();
        </attribute>
    </timer>
</window>




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.