两个线程实现 一个线程专门打印数字, 一个线程专门打印字母12A34B56C78D…5152Z
public class Test {
public static void main(String[] args) {
Resource resource = new Resource();
PrintLetter letter = new PrintLetter(resource);
PrintNumber number = new PrintNumber(resource);
Thread t = new Thread(letter);
Thread t2 = new Thread(number);
t.start();
t2.start();
}
}
class Resource{
boolean flag = true;//打印了字母,该打数字了
// false 打印了数字了,该打字母
// 打印字母
public synchronized void printLetter(int num){
if(flag==true){//打印了字母,该打数字了,所以让字母等着
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
System.out.print((char)num);
// 打印完字母以后, 应该改变flag的状态
// 打印完字母了,该打印数字了
flag = true;
this.notify();
}
//
public synchronized void printNum(int num){
// 什么情况下让数字等着
if(flag == false){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print(num+""+(num+1));//1 3 5
//刚刚打印完数字了,该打字母
flag = false;
this.notify();
}
}
class PrintLetter implements Runnable{
Resource resource;
public PrintLetter(Resource resource) {
super();
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 26; i++) {
resource.printLetter(65+i);
}
}
}
class PrintNumber implements Runnable{
Resource resource;
public PrintNumber(Resource resource) {
super();
this.resource = resource;
}
@Override
public void run() {
for (int i = 1; i < 52; i+=2) {
resource.printNum(i);
}
}
}
结果:
生产者生产面包,消费者消费面包
public class Test2 {
public static void main(String[] args) {
Market market = new Market();
//
Prudoct prudoct = new Prudoct(market);
Custom custom = new Custom(market);
Thread t = new Thread(prudoct);
Thread t2 = new Thread(custom);
t.start();
t2.start();
}
}
// 面包类
class Bread{
String brand;
int id;
public Bread(String brand,int id) {
super();
this.brand = brand;
}
@Override
public String toString() {
return "Bread [brand=" + brand + ", id=" + id + "]";
}
}
// 资源类
class Market{
Set<Bread> set = new HashSet<Bread>();
public synchronized void product(Bread bread) {
if(set.size()>=10) {
try {
this.wait(); // 让生产者等着
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 生产
set.add(bread);// 生产了
System.out.println("生产者生产了一个面包:"+bread);
this.notify(); // 唤醒消费者
}
public synchronized void sale() {
// 没有库存了,不能消费了
if(set.size()<=0) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 还有面包,可以继续消费
Iterator<Bread> iterator = set.iterator();
while(iterator.hasNext()) {
Bread bread = iterator.next();
System.out.println("消费者消费了一个面包:"+bread);
iterator.remove();
break;
}
this.notify();
// List.remove(0);
}
}
class Prudoct implements Runnable{
Market market;
public Prudoct(Market market) {
super();
this.market = market;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
Bread bread = new Bread("达利园", i);
market.product(bread);
}
}
}
class Custom implements Runnable{
Market market;
public Custom(Market market) {
this.market = market;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
market.sale();
}
}
}
结果: