java.lang.Thread.getPriority()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(188)

本文整理了Java中java.lang.Thread.getPriority()方法的一些代码示例,展示了Thread.getPriority()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.getPriority()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:getPriority

Thread.getPriority介绍

[英]Returns this thread's priority.
[中]返回此线程的优先级。

代码示例

代码示例来源:origin: netty/netty

@Override
public int priority() {
  return t.getPriority();
}

代码示例来源:origin: redisson/redisson

@Override
public int priority() {
  return t.getPriority();
}

代码示例来源:origin: alibaba/druid

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;
}

代码示例来源:origin: ctripcorp/apollo

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;
 }
}

代码示例来源:origin: alibaba/canal

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;
}

代码示例来源:origin: apache/zookeeper

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;
  }
}

代码示例来源:origin: lingochamp/FileDownloader

@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;
  }
}

代码示例来源:origin: apache/storm

@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;
  }
}

代码示例来源:origin: alibaba/jstorm

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;
  }
}

代码示例来源:origin: netty/netty

@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;
}

代码示例来源:origin: alibaba/fescar

@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;
  }
}

代码示例来源:origin: redisson/redisson

@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;
}

代码示例来源:origin: google/guava

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());
}

代码示例来源:origin: google/guava

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());
 }
}

代码示例来源:origin: google/guava

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());
}

代码示例来源:origin: google/guava

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('-')));
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * 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);
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * 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);
}

代码示例来源:origin: google/guava

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);
}

代码示例来源:origin: ReactiveX/RxJava

@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());
  }
}

相关文章