class Computer
{
String name = "电脑主机";
boolean flag = false;
public synchronized void set(String name)
{
if(!flag)
try
{
super.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.setName(name);
flag = false;
super.notify();
}
public synchronized void get(String name)
{
if(flag)
try
{
super.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(this.getName());
flag = true;
super.notify();
}
public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
}
}
class Producer implements Runnable
{
Computer computer = null;
public Producer(Computer computer)
{
this.computer = computer;
}
public void run()
{
boolean flag = false;
for(int i=0;i<10;i++)
{
if(flag)
{
this.computer.set("电脑主机");
flag = false;
}
else
{
this.computer.set("显示器");
flag = true;
}
}
}
}
class Consumer implements Runnable
{
Computer computer = null;
public Consumer(Computer computer)
{
this.computer = computer;
}
public void run()
{
for(int i=0;i<10;i++)
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.computer.getName();
}
}public class ComputerTest
{
public static void main(String[] args)
{
Computer comp = new Computer();
Producer pro = new Producer(comp);
Consumer con = new Consumer(comp);
new Thread(pro).start();
new Thread(con).start();
}}出现下列提示:
F:\>javac ComputerTest.java
javac: 找不到文件: ComputerTest.java
用法: javac <options> <source files>
-help 用于列出可能的选项本来就是放在f盘根目录先的嘛,怎么会提示找不到呢?
{
String name = "电脑主机";
boolean flag = false;
public synchronized void set(String name)
{
if(!flag)
try
{
super.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.setName(name);
flag = false;
super.notify();
}
public synchronized void get(String name)
{
if(flag)
try
{
super.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(this.getName());
flag = true;
super.notify();
}
public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
}
}
class Producer implements Runnable
{
Computer computer = null;
public Producer(Computer computer)
{
this.computer = computer;
}
public void run()
{
boolean flag = false;
for(int i=0;i<10;i++)
{
if(flag)
{
this.computer.set("电脑主机");
flag = false;
}
else
{
this.computer.set("显示器");
flag = true;
}
}
}
}
class Consumer implements Runnable
{
Computer computer = null;
public Consumer(Computer computer)
{
this.computer = computer;
}
public void run()
{
for(int i=0;i<10;i++)
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.computer.getName();
}
}public class ComputerTest
{
public static void main(String[] args)
{
Computer comp = new Computer();
Producer pro = new Producer(comp);
Consumer con = new Consumer(comp);
new Thread(pro).start();
new Thread(con).start();
}}出现下列提示:
F:\>javac ComputerTest.java
javac: 找不到文件: ComputerTest.java
用法: javac <options> <source files>
-help 用于列出可能的选项本来就是放在f盘根目录先的嘛,怎么会提示找不到呢?
解决方案 »
- java连接SAP函数【急…………………………………………】
- 急急急啊 【在线等待】 那位高手帮我把附件里的 用户名和密码 找出来【附下载地址】
- socket通信中while(true)的问题
- 使用ObjectOutputStream 向文件进行多次追加写时,如何使后面启动的写入操作不写头标志?
- sql两个问题
- operator > cannot be applied to java.math.BigDecimal,int怎么解决
- 如何通过参数判断远程服务器的CPU?
- 求助:ant教程,高手请给小弟指条路啊
- ECLIPSE如何把自己增加的JDBC类和原程序一起打包,急,高分
- 一个简单问题!送分来了!
- 疯狂java讲义 哪里有全本下载!?
- 在SWT中加入tomcat的控制台,有人会吗?
编译通过了,但是在Eclipse中没有任何反应
在dos下提示如下错误:
F:\>java ComputerTest
Exception in thread "main" java.lang.NoClassDefFoundError: ComputerTest (wrong n
ame: com/test/ComputerTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: ComputerTest. Program will exit.
这句时,进入下面的set方法,
下面的flag=ture没有执行所以flag=false;
所以会执行super.wait(); 一直等待 直到重新获取对象锁 this.computer.set("显示器");
flag = true;public synchronized void set(String name) {
if (!flag)
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName());
flag = true;
super.notify();
}
在run方法里 flag不论ture或者false
this.computer.set()方法中都走
if (!flag)
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
因为,这里的flag为computer的flag变量,总是flase.所以,一直wait().
import java.util.LinkedList;
import java.util.List;class Computer
{
private List<String> products = new LinkedList<String>();
private boolean isWait = false; public String get()
{
if (products.isEmpty())
{
isWait = true;
try
{
synchronized (this)
{
super.wait();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
if (products.isEmpty())
{
return null;
}
String str = products.get(0);
products.remove(0);
return str;
} public void set(String host)
{
products.add(host);
if (isWait)
{
synchronized (this)
{
super.notifyAll();
}
}
}
}class Producer implements Runnable
{
private Computer computer; public Producer(Computer computer)
{
this.computer = computer;
} public void run()
{
boolean flag = false;
for (int i = 0; i < 10; i++)
{
if (flag)
{
this.computer.set("电脑主机" + i);
}
else
{
this.computer.set("显示器" + i);
}
flag = !flag;
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}class Consumer implements Runnable
{
private Computer computer; public Consumer(Computer computer)
{
this.computer = computer;
} public void run()
{
for (int i = 0; i < 10; i++)
{
System.out.println(computer.get());
}
}
}public class Hello
{
public static void main(String[] args)
{
Computer co = new Computer();
Producer pro = new Producer(co);
Consumer con = new Consumer(co);
new Thread(pro).start();
new Thread(con).start();
}
}