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基础)