Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9cc25c739713f3ffdd06046e5713d9baebcb8b7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *    Victor Roldan Betancort - Bug 311840 - OutOfMemory with fast population of revision cache
 */
package org.eclipse.net4j.util.ref;

import org.eclipse.net4j.util.concurrent.Worker;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;

/**
 * @author Eike Stepper
 * @since 2.0
 */
public abstract class ReferenceQueueWorker<T> extends Worker
{
  /**
   * @since 3.0
   */
  public static final int DEFAULT_POLL_MILLIS = 1000 * 60; // One minute

  /**
   * @since 3.0
   */
  public static final int ALL_WORK_PER_POLL = -1;

  /**
   * @since 3.0
   */
  public static final int DEFAULT_MAX_WORK_PER_POLL = ALL_WORK_PER_POLL;

  private ReferenceQueue<T> queue = new ReferenceQueue<T>();

  private long pollMillis;

  private int maxWorkPerPoll;

  public ReferenceQueueWorker()
  {
    setPollMillis(DEFAULT_POLL_MILLIS);
    setMaxWorkPerPoll(DEFAULT_MAX_WORK_PER_POLL);
  }

  public long getPollMillis()
  {
    return pollMillis;
  }

  public void setPollMillis(long pollMillis)
  {
    this.pollMillis = pollMillis;
  }

  public int getMaxWorkPerPoll()
  {
    return maxWorkPerPoll;
  }

  public void setMaxWorkPerPoll(int maxWorkPerPoll)
  {
    this.maxWorkPerPoll = maxWorkPerPoll;
  }

  protected ReferenceQueue<T> getQueue()
  {
    return queue;
  }

  @Override
  protected final void work(WorkContext context) throws Exception
  {
    int count = maxWorkPerPoll;
    if (count == ALL_WORK_PER_POLL)
    {
      count = Integer.MAX_VALUE;
    }

    for (int i = 0; i < count; i++)
    {
      Reference<? extends T> reference = queue.poll();
      if (reference == null)
      {
        break;
      }

      work(reference);
    }

    context.nextWork(pollMillis);
  }

  protected abstract void work(Reference<? extends T> reference);
}

Back to the top