package threadtest;import java.util.*;
import java.lang.*;public class start {
static LinkedList queue=new LinkedList();
static read myread=new read();
static write mywrite=new write();
static boolean ava=false;
public static void main(String[] args) {
myread.start();
mywrite.start();
}
}//开启读者,写者线程
package threadtest;public class read extends Thread{
buffer mybuffer=new buffer();
public void run() {
while(!mybuffer.ToRead().equals("over"))
{mybuffer.ToRead();}
}
}
//读者线程:从queue读出“content0,content2,content19”package threadtest;public class write extends Thread{
buffer mybuffer=new buffer();
public void run() {
for (int i=0 ;i<=20;i++)
{mybuffer.ToWrite("content"+i);
}
mybuffer.ToWrite("over");
}}
//写者线程: 向queue写入“content0,content2,content19”
package threadtest;import java.lang.*;
public class buffer {
start mystart=new start();
public synchronized String ToRead(){
String myString=new String();
try{
while(!mystart.ava)
{System.out.println(" ReadWait");
wait();
}myString=(String)mystart.queue.removeFirst();
System.out.println(" Readremove"+myString);
mystart.ava=false;
notifyAll();}catch(Exception e){System.out.println(e);}
return myString;
}
public synchronized void ToWrite(String i){
try{while(mystart.ava)
{System.out.println("WriteWait");
wait();
}
mystart.queue.addLast(i);System.out.println("Write add last one"+i);
mystart.ava=true;
notifyAll();}catch(Exception e){System.out.println(e);}
}
}
//读,写互斥操作
请问,以上代码为什么不能顺利执行?

解决方案 »

  1.   

    改造了一下:import java.util.LinkedList;public class start {  static LinkedList queue = new LinkedList();  static boolean ava = false;  static buffer buf = new buffer();  public static void main(String[] args) {
        read myread = new read();
        write mywrite = new write();
        myread.start();
        mywrite.start();
      }
    }public class read extends Thread {
      buffer mybuffer = null;  public read() {
        mybuffer = start.buf;
      }  public void run() {
        while (!("over").equals(mybuffer.ToRead())) {
          mybuffer.ToRead();
        }
      }
    }public class write extends Thread {
      buffer mybuffer = null;  public write() {
        mybuffer = start.buf;
      }  public void run() {
        for (int i = 0; i <= 20; i++) {
          mybuffer.ToWrite("content" + i);
        }
        mybuffer.ToWrite("over");
      }
    }public class buffer {
      
      public buffer() {
        
      }  public synchronized String ToRead() {
        String myString = new String("");
        try {      while (!start.ava) {
            System.out.println(" Read Wait");
            start.ava = false;
            wait();
          }      myString = (String)start.queue.removeFirst();
          System.out.println(" Readremove" + myString);
          start.ava = false;
          notify();
        } catch (Exception e) {System.out.println(e);
        }
        return myString;
      }  public synchronized void ToWrite(String i) {
        try {      while (start.ava) {
            System.out.println("Write Wait");
            wait();
          }      start.queue.addLast(i);
          System.out.println("Write add last one " + i);
          start.ava = true;
          notify();    } catch (Exception e) {
          System.out.println(e);
        }
      }
    }主要的问题是,在你的源码中,mybuffer是read、write的成员变量,所以他们在运行时是两个相互独立的
    变量(在运行时他们在内存中的地址也不同),不能相互通信。