请问,线程的优先级有哪些值?(如Thread.setPriority(a),----a的取值范围是多少?)
还有就是为什么在执行t2.join()的时候,线程1并没有等待线程2终止再执行,而是互相争夺CPU的执行权?- package com.zzh.bean;
- class MyThreadDemo implements Runnable{
- public void run(){
- for(int i=0;i<5;i++)
- {
- System.out.println(Thread.currentThread().getName()+"----"+i);
- try {
- Thread.sleep(300);
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }}
- }
- public class ThreadTestDemo {
- public static void main(String args[]) throws InterruptedException{
- Thread t1=new Thread(new MyThreadDemo(),"Thread-1");
- t1.setPriority(5);
- t1.start();
- Thread t2=new Thread(new MyThreadDemo(),"Thread—2");
- t2.start();
- t2.join();
- }
- }
- <font color="red">/*
- * 输出:
- Thread-1
- Thread—2
- Thread-1
- Thread—2
- Thread-1
- Thread—2
- Thread-1
- Thread—2
- Thread-1
- Thread—2
- */</font>
复制代码
|
|