共计 2684 个字符,预计需要花费 7 分钟才能阅读完成。
1、线程常用 API
如何获取线程名称
public final String getName(): 获取线程的名称。
如何给线程设置名称
public final void setName(String name): 设置线程的名称
针对不是 Thread 类的子类中如何获取线程对象名称呢?
public static Thread currentThread(): 返回当前正在执行的线程对象
Thread.currentThread().getName()
将该线程标记为守护线程或用户线程
当正在运行的线程都是守护线程时,Java 虚拟机退出。
该方法必须在启动线程前调用
class ThreadDaemon extends Thread { | |
public void run() {for (int x = 0; x < 100; x++) {System.out.println(getName() + ":" + x); | |
} | |
} | |
} |
public static void main(String[] args) {ThreadDaemon td1 = new ThreadDaemon(); | |
ThreadDaemon td2 = new ThreadDaemon(); | |
td1.setName("关羽"); | |
td2.setName("张飞"); | |
// 设置收获线程 | |
td1.setDaemon(true); | |
td2.setDaemon(true); | |
td1.start(); | |
td2.start(); | |
Thread.currentThread().setName("刘备"); | |
for (int x = 0; x < 5; x++) {System.out.println(Thread.currentThread().getName() + ":" + x); | |
} | |
} |
等待该线程终止
public final void join()
public class ThreadJoin extends Thread { | |
public void run() {for (int x = 0; x < 100; x++) {System.out.println(getName() + ":" + x); | |
} | |
} | |
} |
public static void main(String[] args) {ThreadJoin tj1 = new ThreadJoin(); | |
ThreadJoin tj2 = new ThreadJoin(); | |
ThreadJoin tj3 = new ThreadJoin(); | |
tj1.setName("李渊"); | |
tj2.setName("李世民"); | |
tj3.setName("李元霸"); | |
tj1.start(); | |
try {tj1.join(); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
tj2.start(); | |
tj3.start();} |
如何获取线程对象的优先级?
public final int getPriority(): 返回线程对象的优先级
如何设置线程对象的优先级呢?
public final void setPriority(int newPriority):更改线程的优先级。
注意:
线程默认优先级是 5
线程优先级的范围是:1-10。
线程优先级高仅仅表示线程获取的 CPU 时间片的几率高,但是要在次数比较多,或者 多次运行的时候才能看到比较好的效果。
public static void main(String[] args) {ThreadPriority tp1 = new ThreadPriority(); | |
ThreadPriority tp2 = new ThreadPriority(); | |
ThreadPriority tp3 = new ThreadPriority(); | |
tp1.setName("东方不败"); | |
tp2.setName("岳不群"); | |
tp3.setName("林平之"); | |
// 获取默认优先级 | |
// System.out.println(tp1.getPriority()); | |
// System.out.println(tp2.getPriority()); | |
// System.out.println(tp3.getPriority()); | |
// 设置线程优先级 | |
// tp1.setPriority(100000); | |
// 设置正确的线程优先级 | |
tp1.setPriority(10); | |
tp2.setPriority(1); | |
tp1.start(); | |
tp2.start(); | |
tp3.start();} |
线程休眠
public static void sleep(long millis)
class ThreadSleepDemo {public static void main(String[] args) {ThreadSleep ts1 = new ThreadSleep(); | |
ThreadSleep ts2 = new ThreadSleep(); | |
ThreadSleep ts3 = new ThreadSleep(); | |
ts1.setName("林青霞"); | |
ts2.setName("林志玲"); | |
ts3.setName("林志颖"); | |
ts1.start(); | |
ts2.start(); | |
ts3.start();} | |
} |
class ThreadSleep extends Thread { | |
public void run() {for (int x = 0; x < 100; x++) {System.out.println(getName() + ":" + x + ", 日期:" + new Date()); | |
// 睡眠 | |
// 困了,我稍微休息 1 秒钟 | |
try {Thread.sleep(1000); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
} | |
} | |
} |
礼让线程
public static void yield(): 暂停当前正在执行的线程对象,并执行其他线程。
class ThreadYield extends Thread {@Override | |
public void run() {for (int x = 0; x < 100; x++) {System.out.println(getName() + ":" + x); | |
Thread.yield();} | |
} | |
} |
class ThreadYieldDemo {public static void main(String[] args) {ThreadYield ty1 = new ThreadYield(); | |
ThreadYield ty2 = new ThreadYield(); | |
ty1.setName("林青霞"); | |
ty2.setName("刘意"); | |
ty1.start(); | |
ty2.start();} | |
} |
正文完
星哥玩云-微信公众号
