解决方案 »
- Javac编译时报:错误:仅当显式请求注释处理时才接受类名称*** 是怎么回事啊?
- java打包成jar可执行文件
- 急求org.apache.commons.net.ftp.FTPClient 各超时方法意义
- 如何在一个有n个字符的一位数组中找到包含k个字符组合的全部集合,
- 我该如何用swing实现阿?????
- ant的exec任务executable里命令行/与\的麻烦问题
- 在applet中遇到的奇怪的问题
- 使用过JTable的高手请帮忙,给高分!
- 如何最有效的请教掉List中的所有“null”的对象
- 为啥加入监听器后,页面找不到,404了?
- java new对象
- inputstream 生成多个文件?
public class RunnableCaseDemo03 { public static void main(String[] args) {
Product pro=new Product();
Producer3 producer=new Producer3 (pro);
Consumer3 consumer=new Consumer3 (pro);
new Thread(producer).start();
new Thread(consumer).start();
}
}
class Product {
private String name="Android";
private String company="Google公司";
boolean flag=false;
public synchronized void setInfo (String name,String company){
if (!flag){
try{
super.wait();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
/*就是下面的这四行我打算用else {}框起来的 但是就是得不到我要的效果了 请大家帮忙看看*/
this.setName(name);
this.setCompany(company);
flag=false;
super.notify();
}
public synchronized void getInfo (){
if (flag){
try{
super.wait();
}
catch (InterruptedException e){
e.printStackTrace ();
}
}
System.out.println(this.getName()+"-->"+this.getCompany());
flag=true;
super.notify();
}
public void setName (String name){
this.name=name;
}
public void setCompany(String company){
this.company=company;
}
public String getName(){
return this.name;
}
public String getCompany(){
return this.company;
}
}
class Producer3 implements Runnable{
private Product pro=null;
public Producer3 (Product pro){
this.pro=pro;
}
boolean flag=false;
public void run (){
for (int i=0;i<10;i++){
if (flag){
this.pro.setInfo("Android", "Google公司");
flag=false;
}
else{
this.pro.setInfo("Ios", "Appel公司");
flag=true;
}
}
}
}
class Consumer3 implements Runnable{
private Product pro=null;
public Consumer3 (Product pro){
this.pro=pro;
}
public void run (){
for (int i=0;i<10;i++){
this.pro.getInfo();
}
}
}