一、线程通讯
线程通讯: 一个线程完成了自己的任务时,要通知另外一个线程去完成另外一个任务.
生产者与消费者
wait(): 等待 如果线程执行了wait方法,那么该线程会进入等待的状态,等待状态下的线程必须要被其他线程调用notify方法才能唤醒。
notify(): 唤醒 唤醒线程池等待线程其中的一个。
notifyAll() : 唤醒线程池所有等待 线程。
wait与notify方法要注意的事项:
1. wait方法与notify方法是属于Object对象 的。
2. wait方法与notify方法必须要在同步代码块或者是同步函数中才能 使用。
3. 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 Demo5 {
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 Demo6 extends Thread { boolean flag = true; public Demo6(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) { Demo6 d = new Demo6("狗娃"); 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(); }*/ } } } }
三、守护进程和join方法
守护线程(后台线程):在一个进程中如果只剩下 了守护线程,那么守护线程也会死亡。
一个线程默认都不是守护线程。
setDaemon() 设置线程是否为守护线程,true为守护线程, false为非守护线程。
isDaemon() 判断线程是否为守护线程。
//需求: 模拟QQ下载更新包。
public class Demo7 extends Thread { public Demo7(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) { Demo7 d = new Demo7("后台线程"); 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 Demo8 { public static void main(String[] args) { Mon m = new Mon(); m.start(); } }