Java进阶知识查漏补缺06
Java线程通信之1-100交替打印
package com.cjf.Communication; import static java.lang.Thread.sleep; /** * Created with IntelliJ IDEA. * Description: * Author: Everything * Date: 2020-07-01 * Time: 22:00 */ // 1-100交替打印 //涉及到的三个方法: // wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器。 // notify():一旦执行此方法,就会唤醒被wait的一个线程。 如果有多个线程被wait,就唤醒优先级高的 // notifyAll():一旦执行此方法,就会唤醒所有被wait的线程。 // 说明: // 1. wait(), notify(), notifyALl()三个方法必须使用在同步代码块或同步方法中。 // 2. wait(), notify(), notifyAll()三个方法的调用者必须是同步代码块或同步方法中的同步监视器 // 否则,会出现ILlegaLMonitorStateException异常 // 3. wait(), notify(), notifyAll()三个方法是定义在java.Lang.object类中。 class Number implements Runnable{ private int number = 1 ; @Override public void run() { while (true){ synchronized (this) { notifyAll(); if (number <= 100) { try { sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ "打印:" + number); number++; try { //wait方法会使调用他的对象线程阻塞 //waits会释放锁 wait(); } catch (InterruptedException e) { e.printStackTrace(); } }else { break; } } } } } public class CoTest { public static void main(String[] args) { Number number = new Number(); Thread thread1 = new Thread(number); Thread thread2 = new Thread(number); thread1.setName("Printer1"); thread2.setName("Printer2"); thread1.start(); thread2.start(); } }
Java线程通信之经典生产者消费者问题
package com.cjf.Communication; import java.security.PublicKey; /** * Created with IntelliJ IDEA. * Description: * Author: Everything * Date: 2020-07-01 * Time: 23:01 */ // 经典生产者消费者问题 //共用一个职员类 class Clerk{ private int apple = 0; public synchronized void produceProduct(){//生产产品 if (apple < 20){//监视器是Clerk apple++; System.out.println(Thread.currentThread().getName()+"开始生产第"+apple+"苹果"); this.notify(); }else { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void consumerProduct() {//消费产品 if (apple > 0){//监视器也是Clerk,这样两个线程就共用了一个监视器 System.out.println(Thread.currentThread().getName()+"开始消费第"+apple+"苹果"); apple--; this.notify(); }else { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Producer extends Thread{ private Clerk clerk; public Producer(Clerk clerk) { this.clerk = clerk; } @Override public synchronized void run() { System.out.println(Thread.currentThread().getName()+"开始生产产品..."); while (true){ try { Thread.sleep(10); clerk.produceProduct(); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer extends Thread{ private Clerk clerk; public Consumer(Clerk clerk) { this.clerk = clerk; } @Override public void run() { System.out.println(Thread.currentThread().getName()+"开始消费产品..."); while (true){ try { Thread.sleep(100); clerk.consumerProduct(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ProdAndCust { public static void main(String[] args) { Clerk clerk = new Clerk(); Producer producer = new Producer(clerk); Consumer consumer = new Consumer(clerk); producer.setName("生产者"); consumer.setName("消费者"); producer.start(); consumer.start(); } }