这是代码:
public class Test
{
public static void main(String[] args)
{
ReadThread read = new ReadThread("Read");
WriteThread write = new WriteThread("Write");
// 启动线程
read.start();
write.start();
}
}// 公共数据
class MyData
{
public static int data = 0;
public static boolean hasData = false;
}// 读线程
class ReadThread extends Thread
{
public ReadThread(String str)
{
super(str);
} public void run()
{
for (int i = 0; i < 10; i++)
{
this.get();
}
} public synchronized void get()
{
while (!MyData.hasData)
{
try
{
wait();
}
catch (InterruptedException e) { }
}
System.out.println("        读取的数据为:" + MyData.data);
MyData.hasData = false;
notifyAll();
}
}// 写线程
class WriteThread extends Thread
{
public WriteThread(String str)
{
super(str);
} public void run()
{
for (int i = 1; i < 10; i++)
{
this.put(i);
}
} public synchronized void put(int newData)
{
while (MyData.hasData)
{
try
{
wait();
}
catch (InterruptedException e) { }
}
MyData.data = newData;
MyData.hasData = true;
System.out.println("写入的数据为:" + newData);
notifyAll();
}
}这是输出的结果:
写入的数据为:1
我不知道为什么会这样,这个问题我找了好久,还是没有找出来,请大家帮我看看,谢谢了!!

解决方案 »

  1.   


    public class Test {

    public static void main(String[] args) {
    MyData myData=new MyData();
    ReadThread read = new ReadThread("Read",myData);
    WriteThread write = new WriteThread("Write",myData);
    // 启动线程
    read.start();
    write.start();
    }
    }// 公共数据
    class MyData {
    public static int data = 0;
    public static boolean hasData = false;
    public synchronized void put(int newData) {
    while (MyData.hasData) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    MyData.data = newData;
    MyData.hasData = true;
    System.out.println("写入的数据为:" + newData);
    notifyAll();
    }
    public synchronized void get() {
    while (!MyData.hasData) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    System.out.println("        读取的数据为:" + MyData.data);
    MyData.hasData = false;
    notifyAll();
    }
    }// 读线程
    class ReadThread extends Thread {
    private MyData myData;
    public ReadThread(String str,MyData myData) {
    super(str);
    this.myData=myData;
    }
    public void run() {
    for (int i = 0; i < 10; i++) {
    myData.get();
    }
    }
    }// 写线程
    class WriteThread extends Thread {
    private MyData  myData;
    public WriteThread(String str,MyData myData) {
    super(str);
    this.myData=myData;
    } public void run() {
    for (int i = 1; i < 10; i++) {
    myData.put(i);
    }
    }
    }同步的方法放错地方了
      

  2.   

    应把同步描述加到被线程访问的对象方法上.
    而非线程方法.线程1  -访问->  对象方法
    线程2  -访问->  对象方法(线程1在使用) 
    线程2  -堵塞-
    线程1  -释放->  对象方法  
    线程2  -唤醒- 线程2  -访问->  对象方法
    线程1  -访问->  对象方法(线程1在使用) 
    线程1  -堵塞-
    线程2  -释放->  对象方法  
    线程1  -唤醒- 
    .
    .
    .1个对象方法被访问N个线程,所以,需要同步的是对象方法.
      

  3.   

    发现上面个程序可以运行处结果但有个线程会无限等待所以修改了下public class Test {

    public static void main(String[] args) {
    MyData myData=new MyData();
    ReadThread read = new ReadThread("Read",myData);
    WriteThread write = new WriteThread("Write",myData);
    // 启动线程
    read.start();
    write.start();

    try {
    Thread.sleep(1000);
    read.stop();
    write.stop();
    read.join();
    write.join();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }// 公共数据
    class MyData {
    public static int data = 0;
    public static boolean hasData = false;
    public synchronized void put(int newData) {
    while (MyData.hasData) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    MyData.data = newData;
    MyData.hasData = true;
    System.out.println("写入的数据为:" + newData);
    notifyAll();
    }
    public synchronized void get() {
    while (!MyData.hasData) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    System.out.println("        读取的数据为:" + MyData.data);
    MyData.hasData = false;
    notifyAll();
    }
    }// 读线程
    class ReadThread extends Thread {
    private MyData myData;
    public ReadThread(String str,MyData myData) {
    super(str);
    this.myData=myData;
    }
    public void run() {
    for (int i = 0; i < 10; i++) {
    myData.get();
    }
    }
    }// 写线程
    class WriteThread extends Thread {
    private MyData  myData;
    public WriteThread(String str,MyData myData) {
    super(str);
    this.myData=myData;
    } public void run() {
    for (int i = 1; i < 10; i++) {
    myData.put(i);
    }
    }
    }
      

  4.   


    猜测你的想法,代码修改如下:package occupycpu;public class Test {

    public static void main(String[] args) {
    ReadThread read = new ReadThread("Read");
    WriteThread write = new WriteThread("Write");
    read.setT(write);
    write.setT(read);
    // 启动线程
    read.start();
    write.start();
    }
    }// 公共数据
    class MyData {

    private static int data = 0;
    private static boolean hasData = false; public synchronized static int getData() {
    return data;
    } public synchronized static void setData(int data) {
    MyData.data = data;
    } public synchronized static boolean isHasData() {
    return hasData;
    } public synchronized static void setHasData(boolean hasData) {
    MyData.hasData = hasData;
    }
    }// 读线程
    class ReadThread extends Thread {

    private Thread t = null; public ReadThread(String str) {
    super(str);
    } public void run() {
    for (int i = 0; i < 10; i++) {
    this.get();
    }
    } public void get() {
    while (!MyData.isHasData()) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    }
    System.out.println("        读取的数据为:" + MyData.getData());
    MyData.setHasData(false);
    t.interrupt();
    } public Thread getT() {
    return t;
    } public void setT(Thread t) {
    this.t = t;
    }
    }// 写线程
    class WriteThread extends Thread { private Thread t = null; public WriteThread(String str) {
    super(str);
    } public void run() {
    for (int i = 1; i < 10; i++) {
    this.put(i);
    }
    } public void put(int newData) {
    while (MyData.isHasData()) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    }
    MyData.setData(newData);
    MyData.setHasData(true);
    System.out.println("写入的数据为:" + newData);
    t.interrupt();
    } public Thread getT() {
    return t;
    } public void setT(Thread t) {
    this.t = t;
    }
    }仅供参考。
      

  5.   


    我靠,java code怎么发地,再试试。。public class Test { public static void main(String[] args) {
    }
    }
      

  6.   

    [/Quote]public class Test { public static void main(String[] args) {
    }
    }
      

  7.   


    public class Test { public static void main(String[] args) {
    }
    }
      

  8.   


    简单(基本)(迷糊)类型,比如int,boolean不能用变量同步,只能用方法同步,方法同步也是对象同步,就是任何时刻,一个对象中只能有一个同步方法在执行,如果是其他的变量(类的实例等,怎么称呼来着?),可以用变量同步,很多情况下用变量同步就够了,变量同步只同步一小块需要的代码,这样效率会高一些。其他还有很多,这是基本的。仅供参考。