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读取.PDM文件
- 多线程中每个类都有一个锁吗?
- jfreechart 生成的柱状图
- Java RMI 的问题
- 文件的操作
- 如何从一个网络目录地址搜索文件?
- Java中static修饰的变量问题
- 如何通过FTP 获得SOLIRAS某个路径下(可变)下的文件扩展名相同的文件?急,急!
- 我现在用JAVA做了一个应用程序,但我不会打包,请告诉我打包程序的下载地址,最好有使用方法
- 方法都在方法区,如果我的非静态方法没有使用在堆中的非静态变量,(通常会定义成静态)而我没有把方法定义成静态,为什么还是不能直接用“类名.方法”使用
- 疯狂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();
}
}