<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FThread%2FThread_Priority</id>
		<title>Java Tutorial/Thread/Thread Priority - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FThread%2FThread_Priority"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Priority&amp;action=history"/>
		<updated>2026-04-21T22:39:54Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Priority&amp;diff=2777&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Priority&amp;diff=2777&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:26Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:44, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Priority&amp;diff=2778&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Priority&amp;diff=2778&amp;oldid=prev"/>
				<updated>2010-05-31T15:18:06Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Change Thread Priority ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;A priority tells the operating system how much resource should be given to each thread.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;A high-priority thread is scheduled to receive more time on the CPU than a low-priority thread. A method called Thread.setPriority() sets the priority of a thread. Thread class constants can be used to set these.&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class CounterThread extends Thread {&lt;br /&gt;
  String name;&lt;br /&gt;
  public CounterThread(String name) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.name = name;&lt;br /&gt;
    &lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    int count = 0;&lt;br /&gt;
    while (true) {&lt;br /&gt;
      try {&lt;br /&gt;
        sleep(100);&lt;br /&gt;
      } catch (InterruptedException e) {&lt;br /&gt;
      }&lt;br /&gt;
      if (count == 50000)&lt;br /&gt;
        count = 0;&lt;br /&gt;
      System.out.println(name+&amp;quot;:&amp;quot; + count++);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class MainClass{&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
      CounterThread thread1 = new CounterThread(&amp;quot;thread1&amp;quot;);&lt;br /&gt;
      thread1.setPriority(10);&lt;br /&gt;
      CounterThread thread2 = new CounterThread(&amp;quot;thread2&amp;quot;);&lt;br /&gt;
      thread2.setPriority(1);&lt;br /&gt;
      thread2.start();&lt;br /&gt;
      thread1.start();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Demonstrate thread priorities. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class clicker implements Runnable {&lt;br /&gt;
  int click = 0;&lt;br /&gt;
  Thread t;&lt;br /&gt;
  private volatile boolean running = true;&lt;br /&gt;
  public clicker(int p) {&lt;br /&gt;
    t = new Thread(this);&lt;br /&gt;
    t.setPriority(p);&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    while (running) {&lt;br /&gt;
      click++;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public void stop() {&lt;br /&gt;
    running = false;&lt;br /&gt;
  }&lt;br /&gt;
  public void start() {&lt;br /&gt;
    t.start();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class HiLoPri {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);&lt;br /&gt;
    clicker hi = new clicker(Thread.NORM_PRIORITY + 2);&lt;br /&gt;
    clicker lo = new clicker(Thread.NORM_PRIORITY - 2);&lt;br /&gt;
    lo.start();&lt;br /&gt;
    hi.start();&lt;br /&gt;
    try {&lt;br /&gt;
      Thread.sleep(10000);&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Main thread interrupted.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    lo.stop();&lt;br /&gt;
    hi.stop();&lt;br /&gt;
    // Wait for child threads to terminate.&lt;br /&gt;
    try {&lt;br /&gt;
      hi.t.join();&lt;br /&gt;
      lo.t.join();&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;InterruptedException caught&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Low-priority thread: &amp;quot; + lo.click);&lt;br /&gt;
    System.out.println(&amp;quot;High-priority thread: &amp;quot; + hi.click);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Minimum and Maximum Priority Threads ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Test t = new Test();&lt;br /&gt;
  }&lt;br /&gt;
  public Test() {&lt;br /&gt;
    Runnable runner = new MyRunnable(&amp;quot;First&amp;quot;);&lt;br /&gt;
    Thread t = new Thread(runner);&lt;br /&gt;
    t.setPriority(Thread.MIN_PRIORITY);&lt;br /&gt;
    t.start();&lt;br /&gt;
    runner = new MyRunnable(&amp;quot;Second&amp;quot;);&lt;br /&gt;
    t = new Thread(runner);&lt;br /&gt;
    t.setPriority(Thread.MAX_PRIORITY);&lt;br /&gt;
    t.start();&lt;br /&gt;
  }&lt;br /&gt;
  class MyRunnable implements Runnable {&lt;br /&gt;
    protected String name;&lt;br /&gt;
    public MyRunnable(String tn) {&lt;br /&gt;
      name = tn;&lt;br /&gt;
    }&lt;br /&gt;
    public void run() {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        System.out.println(name);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  This class demonstrates how to set Priority ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
/**&lt;br /&gt;
 * This class demonstrates the use of threads. The main() method is the initial&lt;br /&gt;
 * method invoked by the interpreter. It defines and starts two more threads and&lt;br /&gt;
 * the three threads run at the same time. Note that this class extends Thread&lt;br /&gt;
 * and overrides its run() method. That method provides the body of one of the&lt;br /&gt;
 * threads started by the main() method&lt;br /&gt;
 */&lt;br /&gt;
public class ThreadDemo extends Thread {&lt;br /&gt;
  /**&lt;br /&gt;
   * This method overrides the run() method of Thread. It provides the body for&lt;br /&gt;
   * this thread.&lt;br /&gt;
   */&lt;br /&gt;
  public void run() {&lt;br /&gt;
    for (int i = 0; i &amp;lt; 5; i++)&lt;br /&gt;
      compute();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * This main method creates and starts two threads in addition to the initial&lt;br /&gt;
   * thread that the interpreter creates to invoke the main() method.&lt;br /&gt;
   */&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    // Create the first thread: an instance of this class. Its body is&lt;br /&gt;
    // the run() method above&lt;br /&gt;
    ThreadDemo thread1 = new ThreadDemo();&lt;br /&gt;
    // Create the second thread by passing a Runnable object to the&lt;br /&gt;
    // Thread() construtor. The body of this thread is the run() method&lt;br /&gt;
    // of the anonymous Runnable object below.&lt;br /&gt;
    Thread thread2 = new Thread(new Runnable() {&lt;br /&gt;
      public void run() {&lt;br /&gt;
        for (int i = 0; i &amp;lt; 5; i++)&lt;br /&gt;
          compute();&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // Set the priorities of these two threads, if any are specified&lt;br /&gt;
    if (args.length &amp;gt;= 1)&lt;br /&gt;
      thread1.setPriority(Integer.parseInt(args[0]));&lt;br /&gt;
    if (args.length &amp;gt;= 2)&lt;br /&gt;
      thread2.setPriority(Integer.parseInt(args[1]));&lt;br /&gt;
    // Start the two threads running&lt;br /&gt;
    thread1.start();&lt;br /&gt;
    thread2.start();&lt;br /&gt;
    // This main() method is run by the initial thread created by the&lt;br /&gt;
    // Java interpreter. Now that thread does some stuff, too.&lt;br /&gt;
    for (int i = 0; i &amp;lt; 5; i++)&lt;br /&gt;
      compute();&lt;br /&gt;
    // We could wait for the threads to stop running with these lines&lt;br /&gt;
    // But they aren&amp;quot;t necessary here, so we don&amp;quot;t bother.&lt;br /&gt;
    // try {&lt;br /&gt;
    // thread1.join();&lt;br /&gt;
    // thread2.join();&lt;br /&gt;
    // } catch (InterruptedException e) {}&lt;br /&gt;
    // The Java VM exits only when the main() method returns, and when all&lt;br /&gt;
    // threads stop running (except for daemon threads--see setDaemon()).&lt;br /&gt;
  }&lt;br /&gt;
  // ThreadLocal objects respresent a value accessed with get() and set().&lt;br /&gt;
  // But they maintain a different value for each thread. This object keeps&lt;br /&gt;
  // track of how many times each thread has called compute().&lt;br /&gt;
  static ThreadLocal numcalls = new ThreadLocal();&lt;br /&gt;
  /** This is the dummy method our threads all call */&lt;br /&gt;
  static synchronized void compute() {&lt;br /&gt;
    // Figure out how many times we&amp;quot;ve been called by the current thread&lt;br /&gt;
    Integer n = (Integer) numcalls.get();&lt;br /&gt;
    if (n == null)&lt;br /&gt;
      n = new Integer(1);&lt;br /&gt;
    else&lt;br /&gt;
      n = new Integer(n.intValue() + 1);&lt;br /&gt;
    numcalls.set(n);&lt;br /&gt;
    // Display the name of the thread, and the number of times called&lt;br /&gt;
    System.out.println(Thread.currentThread().getName() + &amp;quot;: &amp;quot; + n);&lt;br /&gt;
    // Do a long computation, simulating a &amp;quot;compute-bound&amp;quot; thread&lt;br /&gt;
    for (int i = 0, j = 0; i &amp;lt; 1000000; i++)&lt;br /&gt;
      j += i;&lt;br /&gt;
    // Alternatively, we can simulate a thread subject to network or I/O&lt;br /&gt;
    // delays by causing it to sleep for a random amount of time:&lt;br /&gt;
    try {&lt;br /&gt;
      // Stop running for a random number of milliseconds&lt;br /&gt;
      Thread.sleep((int) (Math.random() * 100 + 1));&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
    }&lt;br /&gt;
    // Each thread politely offers the other threads a chance to run.&lt;br /&gt;
    // This is important so that a compute-bound thread does not &amp;quot;starve&amp;quot;&lt;br /&gt;
    // other threads of equal priority.&lt;br /&gt;
    Thread.yield();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>