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

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

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

Thread.setPriority介绍

[英]Changes the priority of this thread.

First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.

Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group.
[中]更改此线程的优先级。
首先,调用此线程的checkAccess方法时不带任何参数。这可能会导致抛出SecurityException
否则,此线程的优先级将设置为指定的newPriority和线程线程组的最大允许优先级中的较小者。

代码示例

代码示例来源: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: google/guava

@Override
 public Thread newThread(Runnable runnable) {
  Thread thread = backingThreadFactory.newThread(runnable);
  if (nameFormat != null) {
   thread.setName(format(nameFormat, count.getAndIncrement()));
  }
  if (daemon != null) {
   thread.setDaemon(daemon);
  }
  if (priority != null) {
   thread.setPriority(priority);
  }
  if (uncaughtExceptionHandler != null) {
   thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
  }
  return thread;
 }
};

代码示例来源: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: nostra13/Android-Universal-Image-Loader

@Override
  public Thread newThread(Runnable r) {
    Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
    if (t.isDaemon()) t.setDaemon(false);
    t.setPriority(threadPriority);
    return t;
  }
}

代码示例来源: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: weibocom/motan

@Override
  public Thread newThread(Runnable r) {
    Thread thread = new Thread(threadGroup, r, namePrefix + currentThreadNumber.getAndIncrement(), 0);
    thread.setDaemon(isDaemon);
    thread.setPriority(priority);
    return thread;
  }
}

代码示例来源:origin: org.mockito/mockito-core

/**
 * @param cleanerThread {@code true} if a thread should be started that removes stale entries.
 */
public WeakConcurrentMap(boolean cleanerThread) {
  target = new ConcurrentHashMap<WeakKey<K>, V>();
  if (cleanerThread) {
    thread = new Thread(this);
    thread.setName("weak-ref-cleaner-" + ID.getAndIncrement());
    thread.setPriority(Thread.MIN_PRIORITY);
    thread.setDaemon(true);
    thread.start();
  } else {
    thread = null;
  }
}

代码示例来源: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: 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: 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: 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: skylot/jadx

public static void check(final IUpdateCallback callback) {
  Runnable run = () -> {
    try {
      Release release = checkForNewRelease();
      if (release != null) {
        callback.onUpdate(release);
      }
    } catch (Exception e) {
      LOG.debug("Jadx update error", e);
    }
  };
  Thread thread = new Thread(run);
  thread.setName("Jadx update thread");
  thread.setPriority(Thread.MIN_PRIORITY);
  thread.start();
}

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

t.setPriority(priority);
t.setDaemon(true);
return t;

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

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

代码示例来源: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: spring-projects/spring-framework

/**
 * Template method for the creation of a new {@link Thread}.
 * <p>The default implementation creates a new Thread for the given
 * {@link Runnable}, applying an appropriate thread name.
 * @param runnable the Runnable to execute
 * @see #nextThreadName()
 */
public Thread createThread(Runnable runnable) {
  Thread thread = new Thread(getThreadGroup(), runnable, nextThreadName());
  thread.setPriority(getThreadPriority());
  thread.setDaemon(isDaemon());
  return thread;
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Template method for the creation of a new {@link Thread}.
 * <p>The default implementation creates a new Thread for the given
 * {@link Runnable}, applying an appropriate thread name.
 * @param runnable the Runnable to execute
 * @see #nextThreadName()
 */
public Thread createThread(Runnable runnable) {
  Thread thread = new Thread(getThreadGroup(), runnable, nextThreadName());
  thread.setPriority(getThreadPriority());
  thread.setDaemon(isDaemon());
  return thread;
}

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

protected CentralJobScheduler()
{
  workStealingExecutors = new ConcurrentHashMap<>( 1 );
  topLevelGroup = new TopLevelGroup();
  pools = new ThreadPoolManager( topLevelGroup );
  ThreadFactory threadFactory = new GroupedDaemonThreadFactory( Group.TASK_SCHEDULER, topLevelGroup );
  scheduler = new TimeBasedTaskScheduler( Clocks.nanoClock(), pools );
  // The scheduler thread runs at slightly elevated priority for timeliness, and is started in init().
  schedulerThread = threadFactory.newThread( scheduler );
  int priority = Thread.NORM_PRIORITY + 1;
  schedulerThread.setPriority( priority );
}

代码示例来源: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);
}

相关文章