Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 88fcdadac80d14d8cbaa6c66a97ac48898f277ef (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
/*
 * Copyright (c) 2015 Eike Stepper (Loehne, 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
 */
package org.eclipse.net4j.util.tests;

import org.eclipse.net4j.util.concurrent.ConcurrencyUtil;
import org.eclipse.net4j.util.concurrent.ThreadPool;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * A test for {@link ThreadPool}.
 *
 * @author Eike Stepper
 */
public class ThreadPoolTest extends AbstractOMTest
{
  public void testThreadPool() throws Exception
  {
    final ThreadPool pool = ThreadPool.create("test", 100, 200, 60);

    try
    {
      final int tasks = pool.getMaximumPoolSize() + 100;
      final CountDownLatch latch = new CountDownLatch(tasks);

      for (int i = 0; i < tasks; i++)
      {
        final int n = i;
        msg("scheduling " + n);
        pool.submit(new Runnable()
        {
          public void run()
          {
            msg("started " + n + " (wc=" + pool.getPoolSize() + ")");
            ConcurrencyUtil.sleep(1000);
            latch.countDown();
          }
        });
      }

      latch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
      msg("FINISHED");
    }
    finally
    {
      pool.shutdownNow();
    }
  }
}

Back to the top