Alternative 3: Timer (No Suspend or Resume)"

From Documentation
m (correct highlight (via JWB))
 
Line 3: Line 3:
 
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.
 
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 <tt>onTimer</tt> event listener pops the result to the desktop.
+
The idea is simple. The working thread save the result in a temporary space, and then the <code>onTimer</code> event listener pops the result to the desktop.
  
 
<source lang="java" >
 
<source lang="java" >
Line 30: Line 30:
 
</source>
 
</source>
 
   
 
   
Then, you append the labels in the <tt>onTimer</tt> event listener.
+
Then, you append the labels in the <code>onTimer</code> event listener.
  
 
<source lang="xml" >
 
<source lang="xml" >

Latest revision as of 10:35, 19 January 2022

DocumentationZK Developer's GuideAdvanced ZKLong OperationsAlternative 3: Timer (No Suspend or Resume)
Alternative 3: Timer (No Suspend or Resume)


Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


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>



Last Update : 2022/01/19

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