您的位置:首页 >> 编程开发 >> Java >> J2SE >> 正文
RSS
 

Java之多线程(2)

http://www.rdxx.com 05年07月24日 21:14 JR 我要投稿

关键词: 多线程 , Java , 线程

        Thread.currentThread().getName());

    Thread threadA = new Thread(makeRunnable(), "threadA");
    threadA.start();

    try {
      Thread.sleep(3000);
    }
    catch (InterruptedException x) {}

    System.out.println("in main() - threadA.getPriority()=" +
                       threadA.getPriority());
  }
}


更改线程的优先级:setPriority()
    Thread的setPriority()方法带有一个int作为参数。setPriority()方法可以在启动线程前调用,也可以在运行的时候调用。
    SetPriority.java    ??    用setPriority()更改线程的优先级
public class SetPriority
    extends Object {
  private static Runnable makeRunnable() {
    Runnable r = new Runnable() {
      public void run() {
        for (int i = 0; i < 5; i++) {
          Thread t = Thread.currentThread();
          System.out.println(
              "in run() - priority=" +
              t.getPriority() +
              ", name=" + t.getName());

          try {
            Thread.sleep(2000);
          }
          catch (InterruptedException x) {
            // ignore
          }
        }
      }
    };

    return r;
  }

  public static void main(String[] args) {
    Thread threadA = new Thread(makeRunnable(), "threadA");
    threadA.setPriority(Thread.MAX_PRIORITY);
    threadA.start();

    Thread threadB = new Thread(makeRunnable(), "threadB");
    threadB.setPriority(2);
9 7 3 1 2 3 4 5 6 7 8 9 10 4 8 :

 
 
标签: 多线程 , Java , 线程 打印本文
 
 
  相关资讯
RSS
 
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站