程序如下:package thread01;
class Computers{
int count=0;
int flag=1;//1为可以生产,0可以取走
public synchronized void put(){
if(flag==0){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(120);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
System.out.println(Thread.currentThread().getName()+"生产一台电脑,总共生产 "+count+" 台");
flag=0;
notify();
}
public synchronized void get(){
if(flag==1){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"取走一台电脑");
flag=1;
notify();
}
}
class Customer implements Runnable{
Computers computer;
public Customer( Computers computer){
this.computer=computer;
}
@Override
public void run() {
while(true){
computer.get();
}
}
}class Producter implements Runnable{
Computers computer;
public Producter( Computers computer){
this.computer=computer;
}
@Override
public void run() {
while(true){
computer.put();
}
}
}public class Test03 { /**
* @param args
*/
public static void main(String[] args) {
Computers computer=new Computers();
Producter p=new Producter(computer);
Customer c=new Customer(computer);
new Thread(p,"传智").start();
// new Thread(p,"国信").start();
new Thread(c).start();
//new Thread(c).start();
}}
问题:
1.多线程之生产者与消费者,生产一台,取走一台. 2.如果是一个生产者和一个消费者,程序没有问题,
如果是N个生产者和N个消费者,问题就来了.
发现有时可以生产两台,才取走一台.
发现有时可以生产1台,取走n台.
这是为什么呢?
3.分不是很多,请见谅
class Computers{
int count=0;
int flag=1;//1为可以生产,0可以取走
public synchronized void put(){
if(flag==0){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(120);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
System.out.println(Thread.currentThread().getName()+"生产一台电脑,总共生产 "+count+" 台");
flag=0;
notify();
}
public synchronized void get(){
if(flag==1){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"取走一台电脑");
flag=1;
notify();
}
}
class Customer implements Runnable{
Computers computer;
public Customer( Computers computer){
this.computer=computer;
}
@Override
public void run() {
while(true){
computer.get();
}
}
}class Producter implements Runnable{
Computers computer;
public Producter( Computers computer){
this.computer=computer;
}
@Override
public void run() {
while(true){
computer.put();
}
}
}public class Test03 { /**
* @param args
*/
public static void main(String[] args) {
Computers computer=new Computers();
Producter p=new Producter(computer);
Customer c=new Customer(computer);
new Thread(p,"传智").start();
// new Thread(p,"国信").start();
new Thread(c).start();
//new Thread(c).start();
}}
问题:
1.多线程之生产者与消费者,生产一台,取走一台. 2.如果是一个生产者和一个消费者,程序没有问题,
如果是N个生产者和N个消费者,问题就来了.
发现有时可以生产两台,才取走一台.
发现有时可以生产1台,取走n台.
这是为什么呢?
3.分不是很多,请见谅
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货