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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵卓辉 中级黑马   /  2014-3-2 16:09  /  1228 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

请问,线程的优先级有哪些值?(如Thread.setPriority(a),----a的取值范围是多少?)
还有就是为什么在执行t2.join()的时候,线程1并没有等待线程2终止再执行,而是互相争夺CPU的执行权?
  1. package com.zzh.bean;

  2. class MyThreadDemo implements Runnable{
  3.         public void run(){
  4.                 for(int i=0;i<5;i++)
  5.                 {       
  6.                 System.out.println(Thread.currentThread().getName()+"----"+i);
  7.                 try {
  8.                         Thread.sleep(300);
  9.                 } catch (InterruptedException e1) {
  10.                         // TODO Auto-generated catch block
  11.                         e1.printStackTrace();
  12.                 }
  13.         }}
  14. }
  15. public class ThreadTestDemo {
  16.         public static void main(String args[]) throws InterruptedException{
  17.                 Thread t1=new Thread(new MyThreadDemo(),"Thread-1");
  18.                 t1.setPriority(5);
  19.                 t1.start();       
  20.                 Thread t2=new Thread(new MyThreadDemo(),"Thread—2");
  21.                 t2.start();
  22.                 t2.join();                               
  23.         }
  24. }
  25. <font color="red">/*
  26. * 输出:
  27. Thread-1
  28. Thread—2
  29. Thread-1
  30. Thread—2
  31. Thread-1
  32. Thread—2
  33. Thread-1
  34. Thread—2
  35. Thread-1
  36. Thread—2
  37. */</font>
复制代码



1 个回复

倒序浏览
看看底层源码再来解析“线程的优先级有哪些值”
public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
    }

Thread t1=new Thread("Thread-1");
       t1.setPriority(5);
      System.out.println(t1.MAX_PRIORITY+":::"+t1.MIN_PRIORITY);
结果打印:;10:::1所以有限级的限定有几个值应该懂了吧。

public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }
看看着部分join的代码在看看
void join()
          等待该线程终止。
void join(long millis)
          等待该线程终止的时间最长为 millis 毫秒。
void join(long millis, int nanos)
          等待该线程终止的时间最长为 millis 毫秒 + nanos 纳秒。
通过底层源码和api文档我们了解到该方法是线程等待终止的懂了吗?你所说的可能是多核cpu出现的问题这是对于单cpu编程。具体问题可以参见多核cpu的编程内容
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马