public class Apple implements Runnable {
private int appleCount = 5;
@Override
public void run() {
eatApple();
}
public void eatApple(){
appleCount--;
System.out.println(Thread.currentThread().getName() + "吃了一个苹果,还剩" + appleCount + "个苹果");
}
public static void main(String[] args) {
Apple apple = new Apple();
Thread t1 = new Thread(apple, "小强");
Thread t2 = new Thread(apple, "小明");
Thread t3 = new Thread(apple, "小花");
Thread t4 = new Thread(apple, "小红");
Thread t5 = new Thread(apple, "小黑");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
wait(long timeout, int nanos):类似于wait(long timeout),主要区别是wait(long timeout, int nanos)提供了更高的精度。
notify():随机唤醒一个在相同锁对象上等待的线程。
notifyAll():唤醒所有在相同锁对象上等待的线程。
一个简单的等待唤醒实例:
public class Apple {
//苹果数量
private int appleCount = 0;
/**
* 买苹果
*/
public synchronized void getApple() {
try {
while (appleCount != 0) {
wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "买了5个苹果");
appleCount = 5;
notify();
}
/**
* 吃苹果
*/
public synchronized void eatApple() {
try {
while (appleCount == 0) {
wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "吃了1个苹果");
appleCount--;
notify();
}
}
/**
* 生产者,买苹果
*/
public class Producer extends Thread{
private Apple apple;
public Producer(Apple apple, String name){
super(name);
this.apple = apple;
}
@Override
public void run(){
while (true)
apple.getApple();
}
}
/**
* 消费者,吃苹果
*/
public class Consumer extends Thread{
private Apple apple;
public Consumer(Apple apple, String name){
super(name);
this.apple = apple;
}
@Override
public void run(){
while (true)
apple.eatApple();
}
}
public class Demo {
public static void main(String[] args) {
Apple apple = new Apple();
Producer producer = new Producer(apple,"小明");
Consumer consumer = new Consumer(apple, "小红");
producer.start();
consumer.start();
}
}
public class Apple implements Runnable {
private int appleCount = 5;
@Override
public void run() {
eatApple();
}
public void eatApple(){
appleCount--;
System.out.println(Thread.currentThread().getName() + "吃了一个苹果,还剩" + appleCount + "个苹果");
}
public static void main(String[] args) {
Apple apple = new Apple();
Thread t1 = new Thread(apple, "小强");
Thread t2 = new Thread(apple, "小明");
Thread t3 = new Thread(apple, "小花");
Thread t4 = new Thread(apple, "小红");
Thread t5 = new Thread(apple, "小黑");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
wait(long timeout, int nanos):类似于wait(long timeout),主要区别是wait(long timeout, int nanos)提供了更高的精度。
notify():随机唤醒一个在相同锁对象上等待的线程。
notifyAll():唤醒所有在相同锁对象上等待的线程。
一个简单的等待唤醒实例:
public class Apple {
//苹果数量
private int appleCount = 0;
/**
* 买苹果
*/
public synchronized void getApple() {
try {
while (appleCount != 0) {
wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "买了5个苹果");
appleCount = 5;
notify();
}
/**
* 吃苹果
*/
public synchronized void eatApple() {
try {
while (appleCount == 0) {
wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "吃了1个苹果");
appleCount--;
notify();
}
}
/**
* 生产者,买苹果
*/
public class Producer extends Thread{
private Apple apple;
public Producer(Apple apple, String name){
super(name);
this.apple = apple;
}
@Override
public void run(){
while (true)
apple.getApple();
}
}
/**
* 消费者,吃苹果
*/
public class Consumer extends Thread{
private Apple apple;
public Consumer(Apple apple, String name){
super(name);
this.apple = apple;
}
@Override
public void run(){
while (true)
apple.eatApple();
}
}
public class Demo {
public static void main(String[] args) {
Apple apple = new Apple();
Producer producer = new Producer(apple,"小明");
Consumer consumer = new Consumer(apple, "小红");
producer.start();
consumer.start();
}
}