利用主线程控制子线程
通知
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 windowSale;
public class WindowSale1 { public static int countTicket=10;
public static void main(String[] args) { Window1 window1 = new Window1(); Thread thread=new Thread(window1); thread.start(); while (true){ if (countTicket<=5){ System.out.println("小于5张,请停止售票"); window1.setJudge(false); break; } else {System.out.println("不要停下来");} try{ Thread.sleep(50); }catch (InterruptedException interruptedException){ interruptedException.printStackTrace(); } }
} } class Window1 implements Runnable{ private boolean judge=true; @Override public void run() { while (judge){ try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"售卖一张票"); WindowSale1.countTicket=WindowSale1.countTicket-1; System.out.println("还剩"+WindowSale1.countTicket); } }
public void setJudge(boolean judge) { this.judge = judge; } }
|
线程常用的方法
setName 设置线程的名称,使之与参数name相同
start方法,线程开始执行,Java虚拟机底层调用该线程的start0方法
run调用线程的run方法
setPriority更改线程的优先级
getPriority获得线程的优先级
interrupt中断线程,并不是真正的结束线程,送一一般用于中断休眠的线程
yield:线程的礼让,让出cpu,让其他线程执行,但是礼让的时间不确定,所以不一定成功。资源紧张时成功概率比较大。
join:线程的插队,插队线程一旦插队成功,则肯定先执行完线程所有的任务
可以看出猪猪1吃包子的时候
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 49 50 51 52 53 54
|
public class Test { public static void main(String[] args) throws InterruptedException { Pig pig=new Pig(); Pig1 pig1=new Pig1(); Thread thread=new Thread(pig); Thread thread1=new Thread(pig1); thread.setName("猪猪"); thread1.setName("猪猪1"); thread.start(); thread1.start(); thread1.join(); Thread.sleep(10000); System.out.println("老虎来了"); thread.interrupt(); thread1.interrupt();
} }
class Pig implements Runnable{
@Override public void run() { while (true) { System.out.println("猪猪吃包子"); try { System.out.println("猪猪睡觉"); Thread.sleep(2000); } catch (InterruptedException interruptedException) { System.out.println("猪猪快跑,发生了异常"); break; } } } }
class Pig1 implements Runnable {
@Override public void run() { System.out.println("猪猪1吃包子"); System.out.println("猪猪1睡觉"); for (int i=0;i<5;i++){ System.out.println(i); } } }
|
练习
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
|
public class Test { public static void main(String[] args) throws InterruptedException { int j=0; while (true){ System.out.println(j); j++; if(j==5){ A a=new A(); a.start(); a.join(); System.out.println("子线程结束"); } if (j==10){ System.out.println("主线程结束"); break; } }
}
} class A extends Thread{ @Override public void run(){ for (int i=0;i<10;i++){ System.out.println(i); } } }
|
用户线程与守护线程
用户线程:也叫工作线程,当线程的任务执行完或通知方式结束
守护线程:一般为工作线程服务,当所有用户线程结束,守护线程自动结束,比如垃圾回收机制。
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
|
public class Test { public static void main(String[] args) throws InterruptedException { MyDaemonThread myDaemonThread=new MyDaemonThread(); myDaemonThread.setDaemon(true); myDaemonThread.start(); for (int i=0;i<10;i++){ Thread.sleep(100); System.out.println("主线程运行中"); } } } class MyDaemonThread extends Thread{ @Override public void run() { while (true){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("守护线程运行中"); } } }
|
在守护线程我们没有设计结束的标志,但是守护线程自动结束。