一、线程的通讯
1、线程通讯: 一个线程完成了自己的任务时,要通知另外一个线程去完成另外一个任务
2、
常用方法 | 解释 |
---|---|
wait() | 等待,如果线程执行了wait方法,那么该线程会进入等待的状态,等待状态下的线程必须要被其他线程调用notify方法才能唤醒。 |
notify() | 唤醒,唤醒线程池等待线程其中的一个 |
notifyAll() | 唤醒线程池所有等待线程 |
wait与notify方法要注意的事项:
① wait方法与notify方法是属于Object对象的
② wait方法与notify方法必须要在同步代码块或者是同步函数中才能使用
③ wait方法与notify方法必需要由锁对象调用
//产品类
class Product{
String name; //名字
double price; //价格
boolean flag = false; //产品是否生产完毕的标识,默认情况是没有生产完成
}
//生产者
class Producer extends Thread{
Product p ;//产品
public Producer(Product p) {
this.p = p ;
}
@Override
public void run() {
int i = 0 ;
while(true){
synchronized (p) {
if(p.flag==false){
if(i%2==0){
p.name = "苹果";
p.price = 6.5;
}else{
p.name="香蕉";
p.price = 2.0;
}
System.out.println("生产者生产出了:"+ p.name+" 价格是:"+ p.price);
p.flag = true;
i++;
p.notifyAll(); //唤醒消费者去消费
}else{
//已经生产完毕,等待消费者先去消费
try {
p.wait(); //生产者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
//消费者
class Customer extends Thread{
Product p;
public Customer(Product p) {
this.p = p;
}
@Override
public void run() {
while(true){
synchronized (p) {
if(p.flag==true){ //产品已经生产完毕
System.out.println("消费者消费了"+p.name+" 价格:"+ p.price);
p.flag = false;
p.notifyAll(); // 唤醒生产者去生产
}else{
//产品还没有生产,应该等待生产者先生产。
try{
p.wait(); //消费者也等待了...
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class Demo19.1{
public static void main(String[] args) {
//创建产品
Product p = new Product();
//创建生产对象
Producer producer = new Producer(p);
//创建消费者
Customer customer = new Customer(p);
//调用start方法开启线程
producer.start();
customer.start();
}
}
二、线程的停止
1、停止一个线程我们一般都会通过一个变量去控制的。
2、如果需要停止一个处于等待状态下的线程,那么我们需要通过变量配合notify()或者interrupt()来使用。
public class Demo19.2 extends Thread {
boolean flag = true;
public Demo19.2(String name){
super(name);
}
@Override
public synchronized void run() {
int i = 0 ;
while(flag){
try {
this.wait(); //等待..
} catch (InterruptedException e) {
System.out.println("接收到了异常了....");
}
System.out.println(Thread.currentThread().getName()+":"+i);
i++;
}
}
public static void main(String[] args) {
Demo19.2 d = new Demo19.2("狗娃");
d.setPriority(10);
d.start();
for(int i = 0 ; i<100 ; i++){
System.out.println(Thread.currentThread().getName()+":"+i);
//当主线程的i是80的时候停止狗娃线程
//d.interrupt();// interrupt()根本就是无法停止一个线程。
if(i==80){
d.flag = false;
d.interrupt(); //把线程的等待状态强制清除,被清除状态的线程会接收到一个InterruptedException
/*synchronized (d) {
d.notify();
}*/
}
}
}
}
三、守护线程(后台线程)
在一个进程中如果只剩下了守护线程,那么守护线程也会死亡。一个线程默认都不是守护线程。
需求: 模拟QQ下载更新包。
public class Demo19.3 extends Thread {
public Demo19.3(String name){
super(name);
}
@Override
public void run() {
for(int i = 1 ; i<=100 ; i++){
System.out.println("更新包目前下载"+i+"%");
if(i==100){
System.out.println("更新包下载完毕,准备安装..");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Demo19.3 d = new Demo19.3("后台线程");
d.setDaemon(true); //setDaemon() 设置线程是否为守护线程,true为守护线程,false为非守护线程。
//System.out.println("是守护线程吗?"+ d.isDaemon()); //判断线程是否为守护线程。
d.start();
for(int i = 1 ; i<=100 ; i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
四、join方法
//老妈
class Mon extends Thread{
public void run() {
System.out.println("妈妈洗菜");
System.out.println("妈妈切菜");
System.out.println("妈妈准备炒菜,发现没有酱油了..");
//叫儿子去打酱油
Son s= new Son();
s.start();
try {
s.join(); //加入。 一个线程如果执行join语句,那么就有新的线程加入,执行该语句的线程必须要让步给新加入的线程先完成任务,然后才能继续执行。
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("妈妈继续炒菜");
System.out.println("全家一起吃饭..");
}
}
class Son extends Thread{
@Override
public void run() {
System.out.println("儿子下楼..");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("儿子一直往前走");
System.out.println("儿子打完酱油了");
System.out.println("上楼,把酱油给老妈");
}
}
public class Demo19.4{
public static void main(String[] args) {
Mon m = new Mon();
m.start();
}
}