问题描述:
    现在需要实现主从线程能够互斥的访问一个变量,主线程写入数据,从线程读取数据。实现主写完数据后,从线程读取数据。
    请各位大虾帮忙一下!多谢!

解决方案 »

  1.   

    我用的是JBuilder里面是JDK1.4没有用java.util.concurrent.Exchanger这个类库
    请指教
      

  2.   


    public class Test{  public static void main(String[] args){
      ArrayList<Double> a1=new ArrayList<Double>();
      Semaphore s=new Semaphore(1);
      WriteThread w1=null;
      for(int i=1;i<10;i++){
      w1=new WriteThread("write Thread",s,a1);
      w1.start();
      }
      ReadThread r1=null;
      for(int i=1;i<10;i++){
      r1=new ReadThread("read Thread",s,a1);
      r1.start();
      }
      
      }
    }class WriteThread extends Thread{
    Semaphore s=null;
    List<Double> l=null;
    public WriteThread(String name,Semaphore s,List<Double> l){
    super(name);
    this.s=s;
    this.l=l;
    }
    public void run(){
    try{
    s.acquire(1);
    l.add(Math.random());
    System.out.println(Thread.currentThread().getName());
    s.release(1);
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }
    }
    class ReadThread extends Thread{
    Semaphore s=null;
    List<Double> l=null;
    public ReadThread(String name,Semaphore s,List<Double> l){
    super(name);
    this.s=s;
    this.l=l;
    }
    public void run(){
    try{
    s.acquire(1);
    System.out.println(Thread.currentThread().getName()+l);
    s.release(1);
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }
    }
      

  3.   

    一个static 变量做锁,一个static变量缓冲就行了。
      

  4.   

    线程之间同步要用到notify和wait方法
      

  5.   

    class Productor implements Runnable {
    private TestWaitAndNotify twn;

    public Productor(TestWaitAndNotify twn) {
    this.twn = twn;
    }

    @Override
    public void run() {
    for(int i=0; i<5; i++) {
    twn.writeCount();
    }
    }
    }class Consumer implements Runnable {
    private TestWaitAndNotify twn;
    public Consumer(TestWaitAndNotify twn) {
    this.twn = twn;
    }

    @Override
    public void run() {
    for(int i=0; i<5; i++) {
    twn.readCount();

    }
    }

    }public class TestWaitAndNotify {
    private int count = 1;

    public synchronized void writeCount()  {
    try {
    wait(200); // waiting for consumer
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }

    notify();
    count += 1;
    System.out.println(" -->> Productor: " + count);
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    public synchronized void readCount() {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(" -->> Consumer: " + count);
    notify();
    }

    public static void main(String[] args) {
    final TestWaitAndNotify twn = new TestWaitAndNotify();
    Thread p = new Thread(new Productor(twn));
    Thread c = new Thread(new Consumer(twn));
    p.start();
    c.start();

    }}console result: -->> Productor: 2
     -->> Consumer: 2
     -->> Productor: 3
     -->> Consumer: 3
     -->> Productor: 4
     -->> Consumer: 4
     -->> Productor: 5
     -->> Consumer: 5
     -->> Productor: 6
     -->> Consumer: 6