package gay;
//一个商店有一种商品,边生产边消费,生产二十个就停止,消费二十个就生产
class dian {// 店员
int ii;
public synchronized void add()
{
if(ii>=20)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii++;
System.out.println(Thread.currentThread().getName()+"生产了第"+ii+"个产品");
notify();
}
}
public synchronized void xiao()
{
if(ii<=0)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii--;
System.out.println(Thread.currentThread().getName()+"生产了第"+ii+"个产品");
notify();
}
}
}
class chang implements Runnable{// 工厂
dian dia;
public chang(dian dia) {
this.dia = dia;
}
@Override
public void run() {
System.out.println("开始生产");
while (true) {
dia.add();
}
}
}
class xiao implements Runnable{// 消费
dian dia;
public xiao(dian dia) {
this.dia = dia;
}
public void run() {
System.out.println("消费生产");
while (true) {
dia.xiao();
}
}
}
public class Sded {
public static void main(String[] args) {
dian d=new dian();
chang c=new chang(d);
xiao x=new xiao(d);
Thread thread=new Thread(c);
Thread thread2=new Thread(x);
Thread thread3=new Thread(x);
thread.setName("线程1");
thread2.setName("线程2");
thread3.setName("线程3");
thread.start();
thread2.start();
thread3.start();
}
}
//一个商店有一种商品,边生产边消费,生产二十个就停止,消费二十个就生产
class dian {// 店员
int ii;
public synchronized void add()
{
if(ii>=20)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii++;
System.out.println(Thread.currentThread().getName()+"生产了第"+ii+"个产品");
notify();
}
}
public synchronized void xiao()
{
if(ii<=0)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii--;
System.out.println(Thread.currentThread().getName()+"生产了第"+ii+"个产品");
notify();
}
}
}
class chang implements Runnable{// 工厂
dian dia;
public chang(dian dia) {
this.dia = dia;
}
@Override
public void run() {
System.out.println("开始生产");
while (true) {
dia.add();
}
}
}
class xiao implements Runnable{// 消费
dian dia;
public xiao(dian dia) {
this.dia = dia;
}
public void run() {
System.out.println("消费生产");
while (true) {
dia.xiao();
}
}
}
public class Sded {
public static void main(String[] args) {
dian d=new dian();
chang c=new chang(d);
xiao x=new xiao(d);
Thread thread=new Thread(c);
Thread thread2=new Thread(x);
Thread thread3=new Thread(x);
thread.setName("线程1");
thread2.setName("线程2");
thread3.setName("线程3");
thread.start();
thread2.start();
thread3.start();
}
}