java线程(二)

1.线程的优先级

每一个java线程都有一个优先级,这样有助于操作系统确定线程的调度顺序。

java线程的优先级是一个整数,其取值范围是1-10。

默认情况下每个线程的优先级为5。

具有较高优先级的线程对程序更重要,并且应该在低优先级的线程之前分配处理器资源。但是,线程优先级不能保证线程执行的顺序,而且非常依赖于平台。

public class Runabled implements Runnable {
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}
@Test
public void runnableTest(){
    Thread t1=new Thread(new Runabled(),"thread1");
    Thread t2=new Thread(new Runabled(),"thread2");
    t1.setPriority(1);
    t2.setPriority(10);
    t1.start();
    t2.start();
}

执行结果如下

 thread2:0
thread2:1
thread2:2
thread2:3
thread2:4
thread2:5
thread2:6
thread2:7
thread2:8
thread1:0
thread2:9
thread1:1
thread1:2
thread1:3
thread1:4
thread1:5
thread1:6
thread1:7
thread1:8
thread1:9

2.Thread方法

序号方法描述
1public void start()
使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
2public void run()
如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
3public final void setName(String name)
改变线程名称,使之与参数 name 相同。
4public final void setPriority(int priority)
 更改线程的优先级。
5public final void setDaemon(boolean on)
将该线程标记为守护线程或用户线程。
6public final void join(long millisec)
等待该线程终止的时间最长为 millis 毫秒。
7public void interrupt()
中断线程。
8public final boolean isAlive()
测试线程是否处于活动状态。

测试线程是否处于活动状态。 上述方法是被Thread对象调用的。下面的方法是Thread类的静态方法。

序号方法描述
1public static void yield()
暂停当前正在执行的线程对象,并执行其他线程。
2public static void sleep(long millisec)
在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
3public static boolean holdsLock(Object x)
当且仅当当前线程在指定的对象上保持监视器锁时,才返回 true。
4public static Thread currentThread()
返回对当前正在执行的线程对象的引用。
5public static void dumpStack()
将当前线程的堆栈跟踪打印至标准错误流。

 2.1sleep,join,yield方法介绍

 sleep方法:让线程休眠,Thread类的静态方法。
 

public class Sleep {
    public static void main(String args[])throws Exception{
        MyThead myThead=new MyThead();
        myThead.start();
        myThead.sleep(10000);
        myThead.flag=false;
    }
}
class MyThead extends Thread{
    boolean flag=true;

    @Override
    public void run() {
        while (flag){
            System.out.println("==================="+new Date().toLocaleString()+"===================");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

// 执行结果
===================2019-2-15 13:23:24===================
===================2019-2-15 13:23:25===================
===================2019-2-15 13:23:26===================
===================2019-2-15 13:23:27===================
===================2019-2-15 13:23:28===================
===================2019-2-15 13:23:29===================
===================2019-2-15 13:23:30===================
===================2019-2-15 13:23:31===================
===================2019-2-15 13:23:32===================
===================2019-2-15 13:23:33===================

join方法:调用join方法合并线程,使得线程之间的并行执行变成串行执行

public class Sleep {
    public static void main(String args[])throws Exception{
        MyThead myThead=new MyThead("A");
        MyThead my=new MyThead("B");
        myThead.start();

        myThead.join(3000);//线程A先执行3秒,3秒后交替
        my.start();
    }
}
class MyThead extends Thread{
    MyThead(String name) {
    super(name);
}
    @Override
    public void run() {
      for(int i=0;i<5;i++){
        System.out.println("this is "+Thread.currentThread().getName());
          try {
              sleep(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
              return;
          }
      }
    }
}

执行结果

this is A
this is A
this is A
this is B
this is A
this is B
this is A
this is B
this is B
this is B

 yield方法:使得当前线程的运行状态变成就绪状态。即将下一次执行的机会让掉,让其他或者自己的线程执行,也就是谁先抢到谁执行。

public class Sleep {
    public static void main(String args[])throws Exception{
        MyThead myThead=new MyThead("A");
        MyThead my=new MyThead("B");
        MyThead c=new MyThead("C");
        myThead.start();
        c.start();
        my.start();
    }
}
class MyThead extends Thread{
    MyThead(String name) {
        super(name);
    }
    @Override
    public void run() {
        for(int i=0;i<5;i++){
            System.out.println("this is "+Thread.currentThread().getName());
                if(i%3==0){
                    System.out.println("--------");
                    yield();
                }
        }
    }
}

执行结果

this is A
--------
this is B
--------
this is C
--------
this is B
this is A
this is A
this is A
--------
this is B
this is C
this is A
this is C
this is B
--------
this is C
--------
this is B
this is C

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值