java基础学习笔记之九--线程(2)

线程中的常用方法

  1.sleep(long millis)

    Causes the currently executing thread to sleep (temporarily cease execution).
    在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。
    eg:sleep(1000);//暂停一秒钟

  2.join 

    Waits for this thread to die.
    等待该线程执行完(终止).
    (相当于线程合并eg: a.join(),a线程和当前线程合并,合并后程序会先执行完a线程,然后继续当前线程)

  3.yield (让出自己当前的时间片,让别的线程执行,自己重新去排队)

    Causes the currently executing thread object to temporarily pause and allow other threads to execute.
    暂停当前正在执行的线程对象,让其他线程执行。  

  4.isAlive (线程状态,只要在start启动后,运行结束前都是live)

    Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
    测试线程是否处于活动状态。如果线程已经启动且尚未终止,则为活动状态。 
    返回值(如果该线程处于活动状态,则返回 true;否则返回 false)  

  5.Thread.curretThread 

    Returns a reference to the currently executing thread object.
    返回对当前正在执行的线程对象的引用  

  6.setPriority(int newPriority) 

    Changes the priority of this thread.
    更改线程的优先级
    备注:线程优先级(priority)  
    优先级越高,分到的时间片就越多,优先级默认为5
    Thread.MIN_PRIORITY=1;
    Thread.MAX_PRIORITY=10;
    Thread.NORM_PRIORITY=5 

  7.stop - 不推荐使用

    Forces the thread to stop executing.

    强迫线程停止执行。  

  8.interrupt - 不推荐使用

    Interrupts this thread.

    中断线程。

参考:Java Api  http://docs.oracle.com/javase/6/docs/api/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值