有问题的一段代码,待解决
作者:互联网
package com.www.java2;
/**
*
* @author www
* @creat 2022-{MONTH}-{DAY}
*/
class Clerk{
private int productCount = 0;
public void produceProduct() {
if(productCount < 20){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
productCount++;
System.out.println(Thread.currentThread().getName() + "开始生产第" + productCount + "个产品");
}
}
public void consumeProduct() {
if(productCount > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "开始消费第" + productCount + "个产品");
productCount--;
}
}
}
class Productor extends Thread{
private Clerk clerk;
public Productor(Clerk clerk){
this.clerk = clerk;
}
@Override
public void run() {
System.out.println(getName() + "开始生产产品");
while (true){
clerk.produceProduct();
}
}
}
class Customer extends Thread{
private Clerk clerk;
public Customer(Clerk clerk){
this.clerk = clerk;
}
@Override
public void run() {
System.out.println(getName() + "开始消费产品");
while (true){
clerk.consumeProduct();
}
}
}
public class ProductTest {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Productor p1 = new Productor(clerk);
Customer c1 = new Customer(clerk);
p1.setName("生产者1");
c1.setName("消费者1");
p1.start();
c1.start();
}
}
输出结果:只有生产者,没有消费者
将执行方法挪到run里
package test;
/**
*
* @author www
* @creat 2022-{MONTH}-{DAY}
*/
class Clerk{
// private int productCount = 2;
int productCount = 2;
// public void produceProduct() {
//
// if(productCount < 20){
// try {
// Thread.sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// productCount++;
// System.out.println(Thread.currentThread().getName() + "开始生产第" + productCount + "个产品");
// }
//
//
// }
// public void consumeProduct() {
//
// if(productCount > 0){
// try {
// Thread.sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "开始消费第" + productCount + "个产品");
// productCount--;
// }
//
// }
}
class Productor extends Thread{
private Clerk clerk;
public Productor(Clerk clerk){
this.clerk = clerk;
}
@Override
public void run() {
System.out.println(getName() + "开始生产产品");
while (true){
if(clerk.productCount < 20){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk.productCount++;
System.out.println(Thread.currentThread().getName() + "开始生产第" + clerk.productCount + "个产品");
}
// clerk.produceProduct();
}
}
}
class Customer extends Thread{
private Clerk clerk;
public Customer(Clerk clerk){
this.clerk = clerk;
}
@Override
public void run() {
System.out.println(getName() + "开始消费产品");
while (true){
if(clerk.productCount > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "开始消费第" + clerk.productCount + "个产品");
clerk.productCount--;
}
// clerk.consumeProduct();
}
}
}
public class ProductTest {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Productor p1 = new Productor(clerk);
Customer c1 = new Customer(clerk);
p1.setName("生产者1");
c1.setName("消费者1");
p1.start();
c1.start();
}
}
输出结果:多次运行,都是只在23徘徊
首先不懂第一个为什么消费者进程不运行,第二第一个第二个区别在哪,为什么结果不一样,最后,执行权在第二个中一直交替得到,太有规律,不太正常(针对第三点解决:可能是睡眠时间太短,故将消费睡眠时间改为100,输出结果如下)
发现,消费者消费到最后一个产品之后和第一短代码一样,不再输出了
标签:Thread,Clerk,代码,productCount,public,void,一段,解决,clerk 来源: https://blog.csdn.net/qq_41571545/article/details/123201727