001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.commands;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019:
020: import java.awt.*;
021: import java.util.logging.ConsoleHandler;
022: import java.util.logging.Handler;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025: import java.util.logging.SimpleFormatter;
026:
027: /**
028: * User: Martin
029: * Date: 20.02.2006
030: * Time: 15:02:31
031: */
032: public class TaskCoalescHelper {
033: @SuppressWarnings({"UseOfSystemOutOrSystemErr"})
034: public static void main(@NotNull
035: String[] args) throws InterruptedException {
036: Logger gridvisionLogger = Logger.getLogger("ch.gridvision");//NON-NLS
037: Handler[] handlers = gridvisionLogger.getHandlers();
038: for (Handler handler : handlers) {
039: gridvisionLogger.removeHandler(handler);
040: }
041:
042: ConsoleHandler consoleHandler = new ConsoleHandler();
043: consoleHandler.setFormatter(new SimpleFormatter());
044: consoleHandler.setLevel(Level.ALL);
045: gridvisionLogger.addHandler(consoleHandler);
046:
047: Logger.getLogger("ch.gridvision").setLevel(Level.ALL);//NON-NLS
048:
049: System.out.println("bla");//NON-NLS
050: if (LOG.isLoggable(Level.FINE))
051: LOG.log(Level.FINE, "TaskCoalescHelper.main \"eek\" = "
052: + "eek");
053:
054: TaskCoalescHelper taskCoalescHelper = new TaskCoalescHelper(
055: 100, true);
056: int count = 0;
057: for (int i = 0; i < 100000; i++) {
058: count++;
059: if (Math.random() > 0.99) {
060: count = 0;
061: System.out.println(System.currentTimeMillis());
062: Thread.sleep(5000);
063: }
064: long millis = (long) (Math.random() * 10);
065: System.out.print(".");
066: Thread.sleep(millis);
067: final int cc = count;
068: taskCoalescHelper.addTask(new CoalesceableTask() {
069: @SuppressWarnings({"UseOfSystemOutOrSystemErr"})
070: public void run() {
071: System.out.println("TaskCoalescHelper.run t1 "
072: + System.currentTimeMillis() + " cc = "
073: + cc + "\n");//NON-NLS
074: }
075: });
076: }
077:
078: }
079:
080: @NotNull
081: @NonNls
082: private static final Logger LOG = Logger
083: .getLogger(TaskCoalescHelper.class.getName());
084:
085: private long delay;
086: @Nullable
087: private transient CoalesceableTask nextTask;
088: private transient long submittedMillis;
089:
090: public TaskCoalescHelper(long delay, final boolean inEDT) {
091: this .delay = delay;
092:
093: Thread t = new Thread(new Runnable() {
094: public void run() {
095: //noinspection InfiniteLoopStatement
096: while (true) {
097: //noinspection OverlyBroadCatchBlock
098: try {
099: Runnable nextTask = getNextTask();
100: if (inEDT) {
101: EventQueue.invokeAndWait(nextTask);
102: } else {
103: nextTask.run();
104: }
105: } catch (Throwable e) {
106: if (LOG.isLoggable(Level.FINE))
107: LOG.log(Level.FINE,
108: "TaskCoalescHelper.run ", e);
109: }
110: }
111: }
112: });
113:
114: t.setDaemon(true);
115: t.start();
116: }
117:
118: @Nullable
119: public synchronized CoalesceableTask getNextTask() {
120: while (true) {
121: try {
122: wait();
123: if (nextTask != null) {
124: long timeToWait = delay
125: - (System.currentTimeMillis() - submittedMillis);
126: while (timeToWait > 0) {
127: wait(timeToWait);
128: timeToWait = delay
129: - (System.currentTimeMillis() - submittedMillis);
130: }
131: CoalesceableTask task = nextTask;
132: nextTask = null;
133: return task;
134: }
135: } catch (InterruptedException e) {
136: if (LOG.isLoggable(Level.FINE))
137: LOG.log(Level.FINE,
138: "TaskCoalescHelper.getNextTask ", e);
139: }
140: }
141: }
142:
143: public synchronized void addTask(@NotNull
144: CoalesceableTask task) {
145: submittedMillis = System.currentTimeMillis();
146: nextTask = task;
147: notifyAll();
148: }
149:
150: public abstract static class CoalesceableTask implements Runnable {
151: @NotNull
152: @SuppressWarnings({"UnusedDeclaration"})
153: private StackTraceElement[] callingStackTrace;//useful for debugging
154:
155: public CoalesceableTask() {
156: callingStackTrace = Thread.currentThread().getStackTrace();//MARKED might have a performance impact, remove if necessary
157: }
158:
159: }
160:
161: }
|