在java里,怎么样完成
对一个类对象,在多线程操作时,
多线程可以并发读,
有一个线程写的时候 互斥其它线程写和读?

解决方案 »

  1.   

    简单一个例子可以参照一下。import java.io.IOException;
    import java.nio.CharBuffer;import com.sun.java_cup.internal.Main; class Test {
        private ReadWriteLock rw=new ReadWriteLock();
     private int i = 0;
        private static Test test=new Test();
     public int getI() {
      rw.readLock();
      int ii=i;
      rw.readUnlock();
      return ii;
     } public  void setI(int i) {
       rw.writeLock();
       this.i = i;
       rw.writeUnlock();
      
     } public static Test getInstance()
     {
      return test;
     }} class ReadWriteLock {
     // 读状态
     private boolean isRead; // 写状态
     private boolean isWrite; public synchronized void readLock() {
      // 有写入时读取线程停止
      while (isWrite) {
       try {
        System.out.println("有线程在进行写入,读取线程停止,进入等待状态");
        wait();
       } catch (InterruptedException ex) {
        ex.printStackTrace();
       }
      }  System.out.println("设定锁为读取状态");
      isRead = true;
     } public synchronized void readUnlock() {
      System.out.println("解除读取锁");
      isRead = false;
      notifyAll();
     } public synchronized void writeLock() {
      // 有读取时读取线程停止
      while (isRead) {
       try {
        System.out.println("有线程在进行读取,写入线程停止,进入等待状态");
        wait();
       } catch (InterruptedException ex) {
        ex.printStackTrace();
       }
      }  // 有写入时写入线程也一样要停止
      while (isWrite) {
       try {
        System.out.println("有线程在进行写入,写入线程停止,进入等待状态");
        wait();
       } catch (InterruptedException ex) {
        ex.printStackTrace();
       }
      }  System.out.println("设定锁为写入状态");
      isWrite = true;
     } public synchronized void writeUnlock() {
      System.out.println("解除写入锁");
      isWrite = false;
      notifyAll();
     }
    }
     
     public class MyThread implements Runnable
     {
     public void run() {
      Test.getInstance().setI(1);
      System.out.println(Test.getInstance().getI());
      Test.getInstance().setI(2);
      System.out.println(Test.getInstance().getI());
      Test.getInstance().setI(3);
      System.out.println(Test.getInstance().getI());
      
     }
     public static void main(String[] args) {
      MyThread th=new MyThread();
      Thread thread1=new Thread(th);
      Thread thread2=new Thread(th);
      Thread thread3=new Thread(th);
      thread1.start();
      thread2.start();
      thread3.start();
     } 
     }
      

  2.   

    进程互斥用到synchronized, 楼上的有例子了。
    意思就是,当一个线程在访问的时候,要锁上,不能让别的线程再进来,为了数据安全。
      

  3.   

    简单的互斥,用Lock作为信号量比较简单import java.util.concurrent.locks.*;public class Test implements Runnable
    {
    private Lock lock;
    private String info;
    private static int num = 0; //临界资源

    public Test(String str, Lock l)
    {
    lock = l;
    info = str;
    }

    public void changeNum()
    {
    lock.lock(); //以下三行是临界区
    System.out.println(info + " get " + num);
    num = (int) (Math.random() * 10);
    System.out.println(info + " put " + num);
    lock.unlock();
    }

    public void run()
    {
    while (true)
    {
    changeNum();
    try 
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException e) 
    {
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args)
    {
    ReentrantLock lock = new ReentrantLock();
    Thread t1 = new Thread(new Test("线程1", lock));
    Thread t2 = new Thread(new Test("线程2", lock));
    Thread t3 = new Thread(new Test("线程3", lock));
    t1.start();
    t2.start();
    t3.start();
    }
    }而synchronized这个关键字就相当于给临界区加锁和解锁,所以也可以写成下面这样import java.util.concurrent.locks.*;public class Test implements Runnable
    {
    private Lock lock;
    private String info;
    private static int num = 0; //临界资源

    public Test(String str, Lock l)
    {
    lock = l;
    info = str;
    }

    public synchronized void changeNum()
    {
    //lock.lock(); //以下三行是临界区
    System.out.println(info + " get " + num);
    num = (int) (Math.random() * 10);
    System.out.println(info + " put " + num);
    //lock.unlock();
    }

    public void run()
    {
    while (true)
    {
    changeNum();
    try 
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException e) 
    {
    e.printStackTrace();
    }
    }
    }

    public static void main(String[] args)
    {
    ReentrantLock lock = new ReentrantLock();
    Thread t1 = new Thread(new Test("线程1", lock));
    Thread t2 = new Thread(new Test("线程2", lock));
    Thread t3 = new Thread(new Test("线程3", lock));
    t1.start();
    t2.start();
    t3.start();
    }
    }
      

  4.   

    那个,第二个例子举错了,不好意思,对synchonized还不太清楚,还是第一个例子比较好理解点