黑马程序员技术交流社区
标题:
请教这段关于生产消费的代码出了什么问题
[打印本页]
作者:
tougboy
时间:
2015-5-29 17:10
标题:
请教这段关于生产消费的代码出了什么问题
菜鸟向各位学长请教一段代码 麻烦大家帮忙看看
说是实现生产一个消费一个的流程的,不知道我哪里弄错了 怎么也调不出来 越调越乱
package studing;
class Person{
private String name;
private String sex;
public boolean isempty =true;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
public void set(String name,String sex) throws InterruptedException{
synchronized (this) {
while(isempty){
System.out.println("kaishi wait");
this.wait();
this.name=name;
this.sex=sex;
isempty=false;System.out.println("isempty zhiwei false");
this.notifyAll();
}
}
}
public void get() throws InterruptedException{
synchronized (this) {
System.out.println("isempty "+isempty);
while(!isempty){
System.out.println("get fangfa tongbu nei");
this.wait();
System.out.println("xingming"+getName()+", "+"xingbie"+getSex());
isempty=true;
this.notifyAll();
}
}
}
}
class Consumer implements Runnable{
private Person p;
public Consumer(Person p){
super();
this.p=p;
}
public void run() {
System.out.println("consumer run fangfa");
for(int i=0;i<100;i++){
try {
p.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Producer implements Runnable{
private Person p;
public Producer(Person p) {
super();
this.p=p;
System.out.println("producer gouzao");
}
@Override
public void run() {
System.out.println("producer run fangfa");
for(int i=0;i<100;i++){
if(i%2==0){
try {
System.out.println("set liuzhao nan");
p.set("刘钊", "男");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{try {
System.out.println("set zhangzetian nv");
p.set("章泽天", "女");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
}}
public class shengchanxiaofei {
public static void main(String[] args) {
Person p =new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}
作者:
xgd6612
时间:
2015-5-29 17:50
其实我想说 ,这代码 压根都不想看啊 太长了 看起来太痛苦 一点注释都没有 我想问下 你到底有什么问题 编译没通过,输出结果不对 输出的是什么 这样就不用看全部的代码了~~~~~~~
作者:
tougboy
时间:
2015-5-31 22:08
我也看着头晕 以后再来看望他
作者:
夜愿Relax
时间:
2015-5-31 22:23
还没学到,帮顶个
作者:
mishisanyi
时间:
2015-6-1 12:33
不写注释真是要人命啊
作者:
mishisanyi
时间:
2015-6-1 21:34
package ing;
class Person{
private String name;
private String sex;
public boolean isempty =true;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
public void set(String name,String sex) throws InterruptedException{
synchronized (this) {
while(isempty){
System.out.println("kaishi wait");
this.notifyAll();
this.name=name;
this.sex=sex;
isempty=false;System.out.println("isempty zhiwei false");
this.wait();
break;
}
}
}
public void get() throws InterruptedException{
synchronized (this) {
System.out.println("isempty "+isempty);
while(!isempty){
System.out.println("get fangfa tongbu nei");
this.notifyAll();
System.out.println("xingming"+getName()+", "+"xingbie"+getSex());
isempty=true;
this.wait();
break;
}
}
}
}
class Consumer implements Runnable{
private Person p;
public Consumer(Person p){
super();
this.p=p;
}
public void run() {
System.out.println("consumer run fangfa");
for(int i=0;i<100;i++){
try {
p.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Producer implements Runnable{
private Person p;
public Producer(Person p) {
super();
this.p=p;
System.out.println("producer gouzao");
}
@Override
public void run() {
System.out.println("producer run fangfa");
for(int i=0;i<100;i++){
if(i%2==0){
try {
System.out.println("set liuzhao nan");
p.set("刘钊", "男");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{try {
System.out.println("set zhangzetian nv");
p.set("章泽天", "女");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
}}
public class shengchanxiaofei {
public static void main(String[] args) {
Person p =new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}
class Consumer implements Runnable{
private Person p;
public Consumer(Person p){
super();
this.p=p;
}
public void run() {
System.out.println("consumer run fangfa");
for(int i=0;i<100;i++){
try {
p.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable{
private Person p;
public Producer(Person p) {
super();
this.p=p;
System.out.println("producer gouzao");
}
Thread t = Thread.currentThread();
@Override
public void run() {
System.out.println("producer run fangfa");
for(int i=0;i<100;i++){
if(i%2==0){
try {
System.out.println("set liuzhao nan");
p.set("刘钊", "男");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{try {
System.out.println("set zhangzetian nv");
p.set("章泽天", "女");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
}}
public class shengchanxiaofei {
public static void main(String[] args) {
Person p =new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}
ok,你是不是想达到这个效果,自己看看问题出在哪里
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2