*/
public class ThreadingTest {
+ private static final String LOCAL_NAME = "localName";
// Logger for this class
private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTest.class);
*/
@Test
public void testThreadFactoryInitialization() {
- final ApplicationThreadFactory threadFactory0 = new ApplicationThreadFactory("localName", 0);
- assertNotNull("Failed to create ApplicationThreadFactory threadFactory0", threadFactory0);
- logger.debug(threadFactory0.toString());
+ final ApplicationThreadFactory objUnderTest = new ApplicationThreadFactory(LOCAL_NAME, 0);
+ assertNotNull("Failed to create ApplicationThreadFactory threadFactory0", objUnderTest);
+ logger.debug(objUnderTest.toString());
assertTrue("Failed to name ApplicationThreadFactory threadFactory0",
- threadFactory0.getName().startsWith("Apex-localName"));
- final ApplicationThreadFactory threadFactory1 = new ApplicationThreadFactory("localName", 0);
- assertNotNull("Failed to create ApplicationThreadFactory threadFactory1", threadFactory1);
- logger.debug(threadFactory1.toString());
+ objUnderTest.getName().startsWith("Apex-" + LOCAL_NAME));
+
+ final ApplicationThreadFactory objUnderTest1 = new ApplicationThreadFactory(LOCAL_NAME, 0);
+ assertNotNull("Failed to create ApplicationThreadFactory threadFactory1", objUnderTest1);
+ logger.debug(objUnderTest1.toString());
assertTrue("Failed to name ApplicationThreadFactory threadFactory1",
- threadFactory1.getName().startsWith("Apex-localName"));
+ objUnderTest1.getName().startsWith("Apex-" + LOCAL_NAME));
- testThreadFactory(threadFactory0, 0);
- testThreadFactory(threadFactory1, 1);
+ testThreadFactory(objUnderTest);
+ testThreadFactory(objUnderTest1);
}
/**
* Test thread factory.
*
* @param threadFactory the thread factory
- * @param factoryId the factory id
*/
- private void testThreadFactory(final ApplicationThreadFactory threadFactory, final int factoryId) {
- final List<ThreadingTestThread> threadList = new ArrayList<ThreadingTestThread>();
+ private void testThreadFactory(final ApplicationThreadFactory threadFactory) {
+ final List<ThreadingTestThread> threadList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
- threadList.add(new ThreadingTestThread());
- threadList.get(i).setThread(threadFactory.newThread(threadList.get(i)));
- assertTrue(threadList.get(i).getName().startsWith("Apex-localName"));
- assertTrue(threadList.get(i).getName().contains(":" + i));
- threadList.get(i).getThread().start();
+ final ThreadingTestThread runnable = new ThreadingTestThread();
+ threadList.add(runnable);
+
+ final Thread thread = threadFactory.newThread(runnable);
+ thread.start();
+
}
// Threads should need a little more than 300ms to count to 3
}
for (int i = 0; i < 5; i++) {
- assertTrue("Thread (" + i + ") failed to get count (" + threadList.get(i).getCounter() + ") up to 3",
- threadList.get(i).getCounter() == 3);
+ ThreadingTestThread thread = threadList.get(i);
+ assertTrue(thread.getName().startsWith("Apex-" + LOCAL_NAME));
+ assertTrue(thread.getName().contains(":" + i));
+ assertTrue("Thread (" + i + ") count should be greater than 0 ", thread.getCounter() > 0);
}
}
}
private long counter = -1;
- private Thread thread = null;
-
- /**
- * Sets the thread.
- *
- * @param thread the new thread
- */
- public void setThread(final Thread thread) {
- this.thread = thread;
- }
-
- /**
- * Gets the thread.
- *
- * @return the thread
- */
- public Thread getThread() {
- return thread;
- }
+ private String threadName;
/*
* (non-Javadoc)
*/
@Override
public void run() {
+ this.threadName = Thread.currentThread().getName();
if (logger.isDebugEnabled()) {
- logger.debug("starting threading test thread \"" + thread.getName() + "\" . . .");
+ logger.debug("starting threading test thread \"" + threadName + "\" . . .");
}
while (!interrupted) {
counter++;
if (logger.isDebugEnabled()) {
- logger.debug("in threading test thread \"" + thread.getName() + "\", counter=" + counter + " . . .");
+ logger.debug("in threading test thread \"" + threadName + "\", counter=" + counter + " . . .");
}
- if (!ThreadUtilities.sleep(100)) {
+ if (!ThreadUtilities.sleep(50)) {
interrupted = true;
}
}
if (logger.isDebugEnabled()) {
- logger.debug("stopped threading test thread \"" + thread.getName() + "\"");
+ logger.debug("stopped threading test thread \"" + threadName + "\"");
}
}
* @return the name
*/
public String getName() {
- return thread.getName();
+ return threadName;
}
/**