class Buffer2{
private int value;
private boolean isEmpty = true;
synchronized void put(int i){
while(!isEmpty){
try{
this.wait();
}catch(InterruptedException e){}
}
}
value = i;
isEmpty = false;
notify();
}
synchronized int get(){
while(isEmpty){
try{
this.wait();
}catch(InterruptedException e){}
isEmpty = true;
notify();
return value;
}
}
class put2 extends Thread{
private Buffer2 bf;
public put2(Buffer2 bf){
this.bf = bf;
}
public void run(){
for(int i=1;i<6;i++){
bf.put(i);
System.out.println("put2 put:"+i);
}
}
public static void main(String[]args){
Buffer2 bf = new Buffer2();
(new put2(bf)).start();
(new put2(bf)).start();
}
}
class Get2 extends Thread{
private Buffer2 bf;
public void Get(Buffer2 bf){
this.bf = bf;
}
public void run(){
for(int i=1;i<6;i++)
System.out.println("\t\t Get get:"+bf.get());
}
}编译出错,请高手帮忙一下
private int value;
private boolean isEmpty = true;
synchronized void put(int i){
while(!isEmpty){
try{
this.wait();
}catch(InterruptedException e){}
}
}
value = i;
isEmpty = false;
notify();
}
synchronized int get(){
while(isEmpty){
try{
this.wait();
}catch(InterruptedException e){}
isEmpty = true;
notify();
return value;
}
}
class put2 extends Thread{
private Buffer2 bf;
public put2(Buffer2 bf){
this.bf = bf;
}
public void run(){
for(int i=1;i<6;i++){
bf.put(i);
System.out.println("put2 put:"+i);
}
}
public static void main(String[]args){
Buffer2 bf = new Buffer2();
(new put2(bf)).start();
(new put2(bf)).start();
}
}
class Get2 extends Thread{
private Buffer2 bf;
public void Get(Buffer2 bf){
this.bf = bf;
}
public void run(){
for(int i=1;i<6;i++)
System.out.println("\t\t Get get:"+bf.get());
}
}编译出错,请高手帮忙一下
解决方案 »
- java 字符编码转换问题 求解
- Java、Scanner扫描输入的问题
- 今天看到书中两个疑似错误,贴出来 ,大家讨论讨论
- 设计一个类时,我觉得一个*.java文件里边,没有必要设计在多个类。我都设计成本类里的方法。
- 如何读写剪贴板clipboard中混合着string和image的内容
- JS打印功能的問題
- jsp中的二维数组如何传给页面的<script language="JavaScript">的二维数组?
- 用Applet制作一个链接按钮嵌入网页应该怎么做?
- 有关继承结构中finalize()的调用顺序的问题!
- 通过JAVA 想把SQL 中表的记录打印出 该如何做?谢谢大家
- IDE的使用
- java js 的bbs论坛平台设计
public class Buffer2 {
private int value;
private boolean isEmpty = true; synchronized void put(int i) {
// while (!isEmpty) {
if (!isEmpty) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
value = i;
isEmpty = false;
notify();
} synchronized int get() {
// while (isEmpty) {
if (isEmpty) {
try {
this.wait();
} catch (InterruptedException e) {
}
}
isEmpty = true;
notify();
return value;
// }
}}public class Put2 extends Thread {
private Buffer2 bf; public Put2(Buffer2 bf) {
this.bf = bf;
} public void run() {
for (int i = 1; i < 6; i++) {
bf.put(i);
System.out.println("put2 put:" + i);
}
} public static void main(String[] args) {
Buffer2 bf = new Buffer2();
//(new put2(bf)).start();
//(new put2(bf)).start();
new Put2(bf).start();
new Get2(bf).start();
}
}public class Get2 extends Thread {
private Buffer2 bf;
public Get2(Buffer2 bf) {
//public void Get(Buffer2 bf) {
this.bf = bf;
} public void run() {
for (int i = 1; i < 6; i++)
System.out.println("\t\t Get get:" + bf.get());
}
}