what' wrong with my simple codes on reader/writer problem ,the writer thread never run

Discussion in 'Java' started by DaVinci, Oct 5, 2006.

  1. DaVinci

    DaVinci Guest

    The Problem is the writer never had changes to run?
    but ,why?
    Thanks.
    Codes:
    import java.awt.*;
    import java.util.concurrent.locks.*;
    public class Main
    {

    public static void main(String[] args)
    {
    init();
    for(int i = 0;i< readers.length;i++)
    {
    readers.start();
    }

    for(int j = 0;j<writers.length;j++)
    {
    writers[j].start();
    }

    }
    public static void init()
    {
    int nr = 5;
    readers = new Reader[nr];
    for(int i = 0;i < nr ;i++)
    {
    Reader reader = new Reader(i);
    readers = reader;
    }
    //----------------------------
    int nw = 3;
    writers = new Writer[3];
    for(int i = 0;i< nw;i++)
    {
    Writer writer = new Writer(i);
    writers = writer;
    }
    };
    static class Reader extends Thread
    {
    public Reader(int num)
    {
    numOfReader = num;
    }
    public void run()
    {
    while(true)
    {
    reading();
    }

    }
    public void reading()
    {
    r.lock();
    try
    {
    System.out.println("reader "+numOfReader+" is
    reading ");

    this.sleep(TIMEOFWORK);

    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    finally
    {

    r.unlock();
    }

    }
    private int numOfReader;

    };

    static class Writer extends Thread
    {
    public Writer(int num)
    {
    numOfWriter = num;
    }
    public void run()
    {
    while(true)
    {
    writing(); //???
    }
    }
    public void writing()
    {
    w.lock();
    try
    {

    numOfWriter ++;
    System.out.println("writer "+numOfWriter+" is writing");

    this.sleep(TIMEOFWORK);


    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    finally
    {
    w.unlock();
    }

    }
    private int numOfWriter;

    };

    public static final ReentrantReadWriteLock rwl = new
    ReentrantReadWriteLock();
    public static final Lock r = rwl.readLock();
    public static final Lock w = rwl.writeLock();

    public static Reader[] readers;
    public static Writer[] writers;
    public static final int TIMEOFWORK = 2000;

    }
    DaVinci, Oct 5, 2006
    #1
    1. Advertising

  2. DaVinci

    Piotr Kobzda Guest

    Re: what' wrong with my simple codes on reader/writer problem ,thewriter thread never run

    DaVinci wrote:

    > The Problem is the writer never had changes to run?
    > but ,why?


    The writer have a chance to run, but most often loses in unfair
    competition. Use of the ReentrantReadWriteLock constructed as fair
    should help.


    piotr
    Piotr Kobzda, Oct 5, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soren Kuula
    Replies:
    1
    Views:
    439
    Henry S. Thompson
    Dec 1, 2005
  2. Kevin
    Replies:
    4
    Views:
    410
    Irrwahn Grausewitz
    Oct 17, 2003
  3. Replies:
    2
    Views:
    2,795
    Malcolm
    Aug 20, 2005
  4. Hunk
    Replies:
    10
    Views:
    957
  5. Dmitry Teslenko
    Replies:
    0
    Views:
    613
    Dmitry Teslenko
    Jan 3, 2010
Loading...

Share This Page