@Override public int priority() { return t.getPriority(); }
@Override public int priority() { return t.getPriority(); }
public Thread newThread(Runnable r) { String threadName = nameStart + threadNo.getAndIncrement() + nameEnd; Thread newThread = new Thread(r, threadName); newThread.setDaemon(true); if (newThread.getPriority() != Thread.NORM_PRIORITY) { newThread.setPriority(Thread.NORM_PRIORITY); } return newThread; }
public Thread newThread(Runnable runnable) { Thread thread = new Thread(threadGroup, runnable,// threadGroup.getName() + "-" + namePrefix + "-" + threadNumber.getAndIncrement()); thread.setDaemon(daemon); if (thread.getPriority() != Thread.NORM_PRIORITY) { thread.setPriority(Thread.NORM_PRIORITY); } return thread; } }
public Thread newThread(Runnable r) { Thread t = new Thread(group, r, name + "-" + threadNumber.getAndIncrement(), 0); t.setDaemon(daemon); if (t.getPriority() != Thread.NORM_PRIORITY) { t.setPriority(Thread.NORM_PRIORITY); } t.setUncaughtExceptionHandler(uncaughtExceptionHandler); return t; }
public Thread newThread(Runnable r) { Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (!t.isDaemon()) t.setDaemon(true); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; } }
@Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (t.isDaemon()) t.setDaemon(false); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; } }
@Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, name + "-" + index.getAndIncrement(), 0); if (t.isDaemon()) { t.setDaemon(false); } if (t.getPriority() != Thread.NORM_PRIORITY) { t.setPriority(Thread.NORM_PRIORITY); } t.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER); return t; } }
public Thread newThread(Runnable runnable) { Thread t = new Thread(group, runnable, name + "-" + index.getAndIncrement(), 0); if (t.isDaemon()) t.setDaemon(false); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; } }
@Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { // Doesn't matter even if failed to set. } return t; }
@Override public Thread newThread(Runnable r) { String name = prefix + "_" + counter.incrementAndGet(); if (totalSize > 1) { name += "_" + totalSize; } Thread thread = new FastThreadLocalThread(r, name); thread.setDaemon(makeDaemons); if (thread.getPriority() != Thread.NORM_PRIORITY) { thread.setPriority(Thread.NORM_PRIORITY); } return thread; } }
@Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { // Doesn't matter even if failed to set. } return t; }
public void testBuildMutateBuild() { ThreadFactory factory1 = builder.setPriority(1).build(); assertEquals(1, factory1.newThread(monitoredRunnable).getPriority()); ThreadFactory factory2 = builder.setPriority(2).build(); assertEquals(1, factory1.newThread(monitoredRunnable).getPriority()); assertEquals(2, factory2.newThread(monitoredRunnable).getPriority()); }
public void testPriority_custom() { for (int i = Thread.MIN_PRIORITY; i <= Thread.MAX_PRIORITY; i++) { ThreadFactory factory = builder.setPriority(i).build(); Thread thread = factory.newThread(monitoredRunnable); assertEquals(i, thread.getPriority()); } }
public void testBuildMutate() { ThreadFactory factory1 = builder.setPriority(1).build(); assertEquals(1, factory1.newThread(monitoredRunnable).getPriority()); builder.setPriority(2); // change the state of the builder assertEquals(1, factory1.newThread(monitoredRunnable).getPriority()); }
public void testThreadFactoryBuilder_defaults() throws InterruptedException { ThreadFactory threadFactory = builder.build(); Thread thread = threadFactory.newThread(monitoredRunnable); checkThreadPoolName(thread, 1); Thread defaultThread = Executors.defaultThreadFactory().newThread(monitoredRunnable); assertEquals(defaultThread.isDaemon(), thread.isDaemon()); assertEquals(defaultThread.getPriority(), thread.getPriority()); assertSame(defaultThread.getThreadGroup(), thread.getThreadGroup()); assertSame(defaultThread.getUncaughtExceptionHandler(), thread.getUncaughtExceptionHandler()); assertFalse(completed); thread.start(); thread.join(); assertTrue(completed); // Creating a new thread from the same ThreadFactory will have the same // pool ID but a thread ID of 2. Thread thread2 = threadFactory.newThread(monitoredRunnable); checkThreadPoolName(thread2, 2); assertEquals( thread.getName().substring(0, thread.getName().lastIndexOf('-')), thread2.getName().substring(0, thread.getName().lastIndexOf('-'))); // Building again should give us a different pool ID. ThreadFactory threadFactory2 = builder.build(); Thread thread3 = threadFactory2.newThread(monitoredRunnable); checkThreadPoolName(thread3, 1); assertThat(thread2.getName().substring(0, thread.getName().lastIndexOf('-'))) .isNotEqualTo(thread3.getName().substring(0, thread.getName().lastIndexOf('-'))); }
/** * Tests whether the original priority is not changed if no priority is * specified. */ @Test public void testNewThreadNoPriority() { final ThreadFactory wrapped = EasyMock.createMock(ThreadFactory.class); final Runnable r = EasyMock.createMock(Runnable.class); final int orgPriority = Thread.NORM_PRIORITY + 1; final Thread t = new Thread(); t.setPriority(orgPriority); EasyMock.expect(wrapped.newThread(r)).andReturn(t); EasyMock.replay(wrapped, r); final BasicThreadFactory factory = builder.wrappedFactory(wrapped).build(); assertSame("Wrong thread", t, factory.newThread(r)); assertEquals("Wrong priority", orgPriority, t.getPriority()); EasyMock.verify(wrapped, r); }
/** * Tests whether the priority is set on newly created threads. */ @Test public void testNewThreadPriority() { final ThreadFactory wrapped = EasyMock.createMock(ThreadFactory.class); final Runnable r = EasyMock.createMock(Runnable.class); final Thread t = new Thread(); EasyMock.expect(wrapped.newThread(r)).andReturn(t); EasyMock.replay(wrapped, r); final int priority = Thread.NORM_PRIORITY + 1; final BasicThreadFactory factory = builder.wrappedFactory(wrapped).priority( priority).build(); assertSame("Wrong thread", t, factory.newThread(r)); assertEquals("Wrong priority", priority, t.getPriority()); EasyMock.verify(wrapped, r); }
public void testThreadFactory() throws InterruptedException { final String THREAD_NAME = "ludicrous speed"; final int THREAD_PRIORITY = 1; final boolean THREAD_DAEMON = false; ThreadFactory backingThreadFactory = new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setName(THREAD_NAME); thread.setPriority(THREAD_PRIORITY); thread.setDaemon(THREAD_DAEMON); thread.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER); return thread; } }; Thread thread = builder.setThreadFactory(backingThreadFactory).build().newThread(monitoredRunnable); assertEquals(THREAD_NAME, thread.getName()); assertEquals(THREAD_PRIORITY, thread.getPriority()); assertEquals(THREAD_DAEMON, thread.isDaemon()); assertSame(UNCAUGHT_EXCEPTION_HANDLER, thread.getUncaughtExceptionHandler()); assertSame(Thread.State.NEW, thread.getState()); assertFalse(completed); thread.start(); thread.join(); assertTrue(completed); }
@Test public void normal() { RxThreadFactory tf = new RxThreadFactory("Test", 1); assertEquals("RxThreadFactory[Test]", tf.toString()); Thread t = tf.newThread(Functions.EMPTY_RUNNABLE); assertTrue(t.isDaemon()); assertEquals(1, t.getPriority()); } }