A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胥文 中级黑马   /  2013-2-22 15:28  /  2345 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

假如在主函数中,我们创建3个线程。
我想问的是:主线程和我们创建的线程有没有优先权啊?还是电脑随机的

评分

参与人数 1黑马币 +9 收起 理由
李培根 + 9 赞一个!

查看全部评分

5 个回复

倒序浏览
本帖最后由 铿锵科技 于 2013-2-22 15:54 编辑

默认创建的线程执行权是平等的
但是你可以使用setPriority改变线程执行优先权
例如:
  1. package com.itheima;
  2. public class MyThread extends Thread {
  3.         public static void main(String args[]) {
  4.                 MyThread myThread = new MyThread("myThread");
  5.                 MyThread thread1 = new MyThread("thread1");
  6.                 MyThread thread2 = new MyThread("thread2");
  7.                 MyThread thread3 = new MyThread("thread3");
  8.                 thread1.setPriority(MIN_PRIORITY);//执行权最小
  9.                 thread3.setPriority(MAX_PRIORITY);//执行权最大
  10.                 thread2.setPriority(NORM_PRIORITY);//执行权一般
  11.                 thread1.start();//执行权最小,最先启动
  12.                 thread2.start();//执行权一般,启动第2
  13.                 myThread.start();//默认执行权,启动第3
  14.                 thread3.start();//执行权最大,启动最后
  15.                
  16.         }
  17.         private String name;
  18.         MyThread(String name){
  19.                 this.name=name;
  20.         }
  21.         public void run() {
  22.                 try {
  23.                                 sleep(1000);
  24.                                 System.out.println(this.name+"线程运行");
  25.                         } catch (InterruptedException e) {
  26.                                 // TODO Auto-generated catch block
  27.                                 e.printStackTrace();
  28.                         }
  29.                         
  30.         }
  31. }
复制代码
输出的结果是最后启动的线程最先执行:

thread3线程运行
myThread线程运行
thread2线程运行
thread1线程运行

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
线程的优先级的范围是1-10;
默认所有线程的等级都是5;
可以通过操作Thread类的setPriority方法来改变线程的优先级,java把线程的优先级的三个范围封装成了常量
分别是:MIN_PRIORITY:最小:1
MAX_PRIORITY:10
NORW_PRIORITY:5
更改线程的优先级不如join()/yield()方法有效,他们直接让线程交出执行权,等别人执行完,或者是交出一次,这样更明显一些.

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
public class TestPriority// 线程的优先权 举例
{
    public static void main(String[] args)
    {
    Thread t1 = new Thread(new T1());
    Thread t2 = new Thread(new T2());
    t1.setPriority(Thread.NORM_PRIORITY + 3);//增加优先级
    t1.start();
    t2.start();
    }

}

class T1 implements Runnable
{
    public void run()
    {
    for (int i = 0; i < 1000; i++)
    {
        System.out.println("T1:" + i);
    }
    }
}

class T2 implements Runnable
{
    public void run()
    {
    for (int i = 0; i < 1000; i++)
    {
        System.out.println("---------T2:" + i);
    }
    }
}
优先级越高,就越可能被cpu调度执行该线程,但是对于java中的优先级来说不太可靠,系统自带一个线程的优先级,java程序中对线程所设置的优先级只是给系统一个提示一个建议,至于系统采纳不采纳就不一定了

就像java的垃圾回收机制一个道理  你只是通知到他了 但是他却不一定来的...

回复 使用道具 举报
1.线程优先级范围在1-10之间。
System.out.println(Thread.MAX_PRIORITY); 打印结果:10
System.out.println(Thread.MIN_PRIORITY); 打印结果:1
System.out.println(Thread.NORM_PRIORITY);打印结果:5

2.当创建一个线程时,如果没有设置该线程的优先级,那么该线程的优先级就是父线程的优先级。如果一个线程没有父线程并且没有设置该线程的优先级,那么该线程默认的优先级为NORM_PRIORITY=5.
所以主线程的优先级是5.

3.线程优先级的注意事项。
看一段代码:
class MyThread extends Thread{
public void run(){
  System.out.println("优先级为"+this.getPriority()+"的线程运行");
}
}
class Test {
public static void main(String[] args) {
  for(int i=0;i<5;i++){
   MyThread thread = new MyThread();
   thread.setPriority(1);
   thread.start();
  }
  for(int i=0;i<5;i++){
   MyThread thread = new MyThread();
   thread.setPriority(5);
   thread.start();
  }
  for(int i=0;i<5;i++){
   MyThread thread = new MyThread();
   thread.setPriority(10);
   thread.start();
  }
}
}打印结果:
优先级为5的线程运行
优先级为5的线程运行
优先级为5的线程运行
优先级为10的线程运行
优先级为10的线程运行
优先级为10的线程运行
优先级为10的线程运行
优先级为5的线程运行
优先级为10的线程运行
优先级为5的线程运行
优先级为1的线程运行
优先级为1的线程运行
优先级为1的线程运行
优先级为1的线程运行
优先级为1的线程运行
从这个例子可以看出:
优先级高的线程先运行(不能保证,只是概率大一些)。所以逻辑上不能靠优先级来控制。
回复 使用道具 举报
//定义一个ThreadImp类继承Thread类
class ThreadImp extends Thread{
      public void run(){
             for(int i = 0; i < 100 ; i ++){
                  System.out. println("This is the sub thread running -->" + i );
                   if(i ==50){
                     try{
                      Thread.sleep(3000);//让线程休眠3000毫秒,即 3 分钟。
                            }//Thread类的sleep方法一定要跑出异常
                         catch(Exception e){
                           e.printStackTrace();
                         }
                   }
                 }
          }
}
..............................................................................................
//主函数
class test{
    public static void main(String args[]){
            ThreadImp tr = new ThreadImp();//生成对象
                   tr.setPriority(10);//设置优先级 ,优先级从1到10 依次升高。
                  System.out.println(tr.getPriority());//得到优先级
                  tr.start();
                for (int j = 0; j < 100 ; j ++){
                 System.out.println("this is the main thread " + j);
                }
        }
}
给你一个例子吧。运行一下,你就能看到区别了。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马