多线程_深度观察状态
线程的优先级
priority
Java提供一个线程调度器来监控控制程序中启动后进入就绪状态的所有线程。线程调度器按照线程的优先级决定应调度那个线程来执行。
- Thread.MIN_PRIORITY = 1
- Thread.MAX_PRIORITY = 10
- Thread.NORM_PRIORITY = 5
使用下述方法获得或设置线程对象优先级。
- int getPriority();
- void setPriority(int newPriority);
优先级的设定建议在start()调用前
注意:优先级低只是意味着获得调度的概率低。并不是绝对调用优先级高后调用优先级低的线程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package cn.Thread;
public class PriorityTest {
public static void main(String[] args) { System.out.println(Thread.currentThread().getPriority()); MyPriority mp = new MyPriority(); Thread t1 = new Thread(mp,"adidas"); Thread t2 = new Thread(mp,"NIKE"); Thread t3 = new Thread(mp,"回力"); Thread t4 = new Thread(mp,"李宁"); Thread t5 = new Thread(mp,"双星"); Thread t6 = new Thread(mp,"puma");
t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.MAX_PRIORITY); t4.setPriority(Thread.MIN_PRIORITY); t5.setPriority(Thread.MIN_PRIORITY); t6.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); } }
class MyPriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName() + "-->"+Thread.currentThread().getPriority()); Thread.package cn.Thread;
public class PriorityTest {
public static void main(String[] args) { System.out.println(Thread.currentThread().getPriority()); MyPriority mp = new MyPriority(); Thread t1 = new Thread(mp,"adidas"); Thread t2 = new Thread(mp,"NIKE"); Thread t3 = new Thread(mp,"回力"); Thread t4 = new Thread(mp,"李宁"); Thread t5 = new Thread(mp,"双星"); Thread t6 = new Thread(mp,"puma");
t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.MAX_PRIORITY); t4.setPriority(Thread.MIN_PRIORITY); t5.setPriority(Thread.MIN_PRIORITY); t6.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); } }
class MyPriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName() + "-->"+Thread.currentThread().getPriority()); Thread.yield(); } }
|
多线程_守护线程
- 线程分为用户线程和守护线程;
- 虚拟机必须确保用户线程执行完毕;
- 虚拟机不用等待守护线程执行完毕;
- 如后台记录操作日志,监控内存使用等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package cn.Thread;
public class DaemonTest {
public static void main(String[] args) { God god = new God(); Youu you = new Youu(); Thread t = new Thread(god); t.setDaemon(true); t.start(); new Thread(you).start(); } }
class Youu implements Runnable { @Override public void run() { for (int i = 0; i <= 365; i++) { System.out.println("happy life......"); } System.out.println("oooooo......"); } }
class God implements Runnable{ @Override public void run() { for (; true; ) { System.out.println(package cn.Thread;
public class DaemonTest {
public static void main(String[] args) { God god = new God(); Youu you = new Youu(); Thread t = new Thread(god); t.setDaemon(true); t.start(); new Thread(you).start(); } }
class Youu implements Runnable { @Override public void run() { for (int i = 0; i <= 365; i++) { System.out.println("happy life......"); } System.out.println("oooooo......"); } }
class God implements Runnable{ @Override public void run() { for (; true; ) { System.out.println("bless you ......"); } } }
|
多线程_基本信息_网红思维[Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package cn.Thread;
public class InfoTest {
public static void main(String[] args) { System.out.println(Thread.currentThread().isAlive()); MyInfo info = new MyInfo("战斗机"); Thread t = new Thread(info); t.setName("公鸡"); t.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(t.isAlive()); }
}
class MyInfo implements Runnable{ private String name; public MyInfo(String name) { this.name = name; }
@Override public void run() { System.out.println(Thread.currentThread().getName() + package cn.Thread;
public class InfoTest {
public static void main(String[] args) { System.out.println(Thread.currentThread().isAlive()); MyInfo info = new MyInfo("战斗机"); Thread t = new Thread(info); t.setName("公鸡"); t.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(t.isAlive()); }
}
class MyInfo implements Runnable{ private String name; public MyInfo(String name) { this.name = name; }
@Override public void run() { System.out.println(Thread.currentThread().getName() + "-->" + name); } }
|