我想实现20个线程随即分配写入和读出,当一个线程写入的时候,该线程休眠5秒,其间别的线程不能写入或读出,一个线程读出的时候,别的线程可以写入或读出。但我实现时输出信息的时候老错误,比如输出“线程-x写入”,立即输出了“线程-x读出”。不知道怎么解决,请帮帮忙。具体代码:
 public class MuitipleThread { public static void main(String[] args) { Buffer sharedLocation = new UnsynchronizedBuffer(); Producer p = new Producer(sharedLocation);
Consumer s = new Consumer(sharedLocation);
for (int i = 1; i <= 20; i++) {
int j = 0;
j = (int) (Math.random() * 10 + 1);
if (j <= 5) {
Thread tdj = new Thread(p);
tdj.start();
} else {
Thread tdj = new Thread(s);
tdj.start();
}
} } interface Buffer {
public void set(); public void get();
} static class Consumer extends Thread {
private Buffer ShareLocation; public Consumer(Buffer shared) { ShareLocation = shared; } public void run() { synchronized (ShareLocation) { try {
while (Producer.i != 0) {
ShareLocation.wait();
} ShareLocation.get();
System.out.println(Thread.currentThread().getName()
+ "读出完成"); } catch (Exception e) { e.printStackTrace();
} } } } static class Producer extends Thread { private Buffer ShareLocation;
public static int i = 1; public Producer(Buffer shared) {
ShareLocation = shared; } public void run() { synchronized (ShareLocation) {
try { ShareLocation.set();
i = 0;
ShareLocation.notify(); } catch (Exception e) { e.printStackTrace();
} } } } static class UnsynchronizedBuffer implements Buffer { public void set() { try { System.err.println(Thread.currentThread().getName() + "写入");
Thread.sleep(5000);
System.err.println(Thread.currentThread().getName() + "写入完成"); } catch (Exception e) {
// TODO: handle exception
} } public void get() {
try { System.out.println(Thread.currentThread().getName() + "读出"); } catch (Exception e) {
// TODO: handle exception
}
}
}
}

解决方案 »

  1.   

    不要这样发代码,这样别人看着不方便public class MuitipleThread {public static void main(String[] args) {Buffer sharedLocation = new UnsynchronizedBuffer();Producer p = new Producer(sharedLocation);
    Consumer s = new Consumer(sharedLocation);
    for (int i = 1; i <= 20; i++) {
    int j = 0;
    j = (int) (Math.random() * 10 + 1);
    if (j <= 5) {
    Thread tdj = new Thread(p);
    tdj.start();
    } else {
    Thread tdj = new Thread(s);
    tdj.start();
    }
    }}interface Buffer {
    public void set();public void get();
    }static class Consumer extends Thread {
    private Buffer ShareLocation;public Consumer(Buffer shared) {ShareLocation = shared;}public void run() {synchronized (ShareLocation) {try {
    while (Producer.i != 0) {
    ShareLocation.wait();
    }ShareLocation.get();
    System.out.println(Thread.currentThread().getName()
    + "读出完成");} catch (Exception e) {e.printStackTrace();
    }}}}static class Producer extends Thread {private Buffer ShareLocation;
    public static int i = 1;public Producer(Buffer shared) {
    ShareLocation = shared;}public void run() {synchronized (ShareLocation) {
    try {ShareLocation.set();
    i = 0;
    ShareLocation.notify();} catch (Exception e) {e.printStackTrace();
    }}}}static class UnsynchronizedBuffer implements Buffer {public void set() {try {System.err.println(Thread.currentThread().getName() + "写入");
    Thread.sleep(5000);
    System.err.println(Thread.currentThread().getName() + "写入完成");} catch (Exception e) {
    // TODO: handle exception
    }}public void get() {
    try {System.out.println(Thread.currentThread().getName() + "读出");} catch (Exception e) {
    // TODO: handle exception
    }
    }
    }
    }
      

  2.   


    public static void main(String[] args) {Buffer sharedLocation = new UnsynchronizedBuffer();Producer p = new Producer(sharedLocation);
    Consumer s = new Consumer(sharedLocation);
    for (int i = 1; i <= 20; i++) {
    int j = 0;
    j = (int) (Math.random() * 10 + 1);
    if (j <= 5) {
    Thread tdj = new Thread(p);
    tdj.start();
    } else {
    Thread tdj = new Thread(s);
    tdj.start();
    }
    }}interface Buffer {
    public void set();public void get();
    }static class Consumer extends Thread {
    private Buffer ShareLocation;public Consumer(Buffer shared) {ShareLocation = shared;}public void run() {synchronized (ShareLocation) {try {
    while (Producer.i != 0) {
    ShareLocation.wait();
    }ShareLocation.get();
    System.out.println(Thread.currentThread().getName()
    + "读出完成");} catch (Exception e) {e.printStackTrace();
    }}}}static class Producer extends Thread {private Buffer ShareLocation;
    public static int i = 1;public Producer(Buffer shared) {
    ShareLocation = shared;}public void run() {synchronized (ShareLocation) {
    try {ShareLocation.set();
    i = 0;
    ShareLocation.notify();} catch (Exception e) {e.printStackTrace();
    }}}}static class UnsynchronizedBuffer implements Buffer {public void set() {try {System.err.println(Thread.currentThread().getName() + "写入");
    Thread.sleep(5000);
    System.err.println(Thread.currentThread().getName() + "写入完成");} catch (Exception e) {
    // TODO: handle exception
    }}public void get() {
    try {System.out.println(Thread.currentThread().getName() + "读出");} catch (Exception e) {
    // TODO: handle exception
    }
    }
    }
    }
      

  3.   

    1、你的代码我看了下,你的代码并没有实现"一个线程读出的时候,别的线程可以写入或读出",你读取的时候加锁了,其它线程无论是写还是读也是无法操作的
    2、除了第一点外你的代码是正确的,出现"比如输出'线程-x写入',立即输出了'线程-x读出'"是由于你输出时使用了两个输出流,这两个流输出的顺序是不可控的,你测试下下面的代码就知道了.多执行几次,所以你把输出统一下就会得到你想看到的结果了public static void main(String[] args) {
    for(int i=0;i<10;i++){
    System.err.println(i);
    }
    System.out.println("correct");
    for(int i=0;i<10;i++){
    System.err.println(i);
    }
    }