|
|
@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.common.threadutils; |
|
|
|
import org.apache.dolphinscheduler.common.thread.Stopper; |
|
|
|
import org.apache.dolphinscheduler.common.thread.Stopper; |
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadPoolExecutors; |
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadPoolExecutors; |
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadUtils; |
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadUtils; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
@ -31,13 +32,14 @@ import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
public class ThreadUtilsTest { |
|
|
|
public class ThreadUtilsTest { |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ThreadUtilsTest.class); |
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ThreadUtilsTest.class); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* create a naming thread |
|
|
|
* create a naming thread |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNewDaemonFixedThreadExecutor() { |
|
|
|
public void testNewDaemonFixedThreadExecutor() { |
|
|
|
// create core size and max size are all 3
|
|
|
|
// create core size and max size are all 3
|
|
|
|
ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread",3); |
|
|
|
ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread", 3); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) { |
|
|
|
for (int i = 0; i < 2; i++) { |
|
|
|
final int index = i; |
|
|
|
final int index = i; |
|
|
@ -73,8 +75,8 @@ public class ThreadUtilsTest { |
|
|
|
// send heart beat work
|
|
|
|
// send heart beat work
|
|
|
|
while (start.getTime().getTime() <= end.getTime().getTime()) { |
|
|
|
while (start.getTime().getTime() <= end.getTime().getTime()) { |
|
|
|
System.out.println("worker here"); |
|
|
|
System.out.println("worker here"); |
|
|
|
System.out.println(index ++); |
|
|
|
System.out.println(index++); |
|
|
|
start.add(Calendar.SECOND, 1); |
|
|
|
start.add(Calendar.SECOND, 1); |
|
|
|
globalTimer.add(Calendar.SECOND, 1); |
|
|
|
globalTimer.add(Calendar.SECOND, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -102,19 +104,17 @@ public class ThreadUtilsTest { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* test threadPoolExecutors with 3 workers and current each 5 tasks |
|
|
|
* test threadPoolExecutors with 3 workers and current each 5 tasks |
|
|
|
* @throws InterruptedException |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testThreadInfo() throws InterruptedException { |
|
|
|
public void testThreadInfo() throws InterruptedException { |
|
|
|
ThreadPoolExecutors workers = ThreadPoolExecutors.getInstance("worker", 1); |
|
|
|
ThreadPoolExecutors workers = ThreadPoolExecutors.getInstance("worker", 1); |
|
|
|
for (int i = 0; i < 2; ++i ) { |
|
|
|
for (int i = 0; i < 2; ++i) { |
|
|
|
int index = i; |
|
|
|
int index = i; |
|
|
|
workers.execute(() -> { |
|
|
|
workers.execute(() -> { |
|
|
|
for (int j = 0; j < 1; ++j) { |
|
|
|
for (int j = 0; j < 1; ++j) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
Thread.sleep(100); |
|
|
|
Thread.sleep(100); |
|
|
|
logger.info("worker %s is doing the task", index); |
|
|
|
logger.info("worker %s is doing the task", index); |
|
|
|
// workers.printStatus();
|
|
|
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
} catch (InterruptedException e) { |
|
|
|
logger.error("InterruptedException", e); |
|
|
|
logger.error("InterruptedException", e); |
|
|
|
} |
|
|
|
} |
|
|
@ -129,7 +129,7 @@ public class ThreadUtilsTest { |
|
|
|
* test a single daemon thread pool |
|
|
|
* test a single daemon thread pool |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNewDaemonSingleThreadExecutor() { |
|
|
|
public void testNewDaemonSingleThreadExecutor() { |
|
|
|
ExecutorService threadTest = ThreadUtils.newDaemonSingleThreadExecutor("thread_test"); |
|
|
|
ExecutorService threadTest = ThreadUtils.newDaemonSingleThreadExecutor("thread_test"); |
|
|
|
threadTest.execute(() -> { |
|
|
|
threadTest.execute(() -> { |
|
|
|
for (int i = 0; i < 2; ++i) { |
|
|
|
for (int i = 0; i < 2; ++i) { |
|
|
@ -148,7 +148,7 @@ public class ThreadUtilsTest { |
|
|
|
ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest-"); |
|
|
|
ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest-"); |
|
|
|
Thread thread1 = threadPoolExecutor.getThreadFactory().newThread(() -> { |
|
|
|
Thread thread1 = threadPoolExecutor.getThreadFactory().newThread(() -> { |
|
|
|
for (int i = 0; i < 2; ++i) { |
|
|
|
for (int i = 0; i < 2; ++i) { |
|
|
|
logger.info("this task is with index " + i ); |
|
|
|
logger.info("this task is with index " + i); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
assertTrue(thread1.getName().startsWith("threadTest-")); |
|
|
|
assertTrue(thread1.getName().startsWith("threadTest-")); |
|
|
@ -160,7 +160,7 @@ public class ThreadUtilsTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNewDaemonCachedThreadPoolWithThreadNumber() { |
|
|
|
public void testNewDaemonCachedThreadPoolWithThreadNumber() { |
|
|
|
ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest--", 3, 10); |
|
|
|
ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest--", 3, 10); |
|
|
|
for (int i = 0; i < 2; ++ i) { |
|
|
|
for (int i = 0; i < 2; ++i) { |
|
|
|
threadPoolExecutor.getThreadFactory().newThread(() -> { |
|
|
|
threadPoolExecutor.getThreadFactory().newThread(() -> { |
|
|
|
assertEquals(3, threadPoolExecutor.getActiveCount()); |
|
|
|
assertEquals(3, threadPoolExecutor.getActiveCount()); |
|
|
|
logger.info("this task is first work to do"); |
|
|
|
logger.info("this task is first work to do"); |
|
|
@ -172,5 +172,4 @@ public class ThreadUtilsTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|