Thread safe resource

Discussion in 'Java' started by Rob Shepherd, May 28, 2004.

  1. Rob Shepherd

    Rob Shepherd Guest

    Dear c.l.j.p,

    The code I attach demonstates thread unsafeness. To summarise; Two threads battle it out
    to read and write to the four byte arrays in the SynchroTest object.
    The output shows partially modified byte arrays can be read...

    I wish to discover how i can make my threaded code safe with minimum of synchronisation
    overhead. I would also like to treat each byte array with separate synchronisation, ie i
    would not like to synchronise using s's LOCK, enabling a thread to write/read from a byte
    array if the opposing Thread is accessing another....

    I presume synchronised blocks are the way to go but is there a way of doing it without
    making a get and set method for each byte array?

    many thanks

    Rob
    --------------------------
    import java.util.*;

    public class SynchroTest
    {
    public byte[] c1 = new byte[6];
    public byte[] c2 = new byte[6];
    public byte[] c3 = new byte[6];
    public byte[] c4 = new byte[6];

    public static void main(String args[]) //make 2 equal threads
    {
    SynchroTest s = new SynchroTest();
    new modThread(s, 1).start();
    new modThread(s, 2).start();
    }

    public static class modThread extends Thread
    {
    int m;
    SynchroTest s;
    Random rand = new Random();

    public modThread(SynchroTest ss, int id)
    {
    s = ss;
    m = id;
    this.setPriority(Thread.MAX_PRIORITY); // higher interrupt power
    }

    public void run()
    {
    while(true)
    {
    int i = rand.nextInt(400);
    byte[] b;
    String l= null;
    if(i<100) // select a random byte array to modify
    {
    b = s.c1;
    l="c1";
    }
    else if(i < 200)
    {
    b = s.c2;
    l="c2";
    }
    else if(i < 300)
    {
    b = s.c3;
    l="c3";
    }
    else
    {
    b = s.c4;
    l="c4";
    }

    if(rand.nextInt(50) < 25) //get a byte array
    {
    System.out.println(m+": Get " +l + " " + b[0]+ " " + b[1]+ " " +
    b[2]+ " " + b[3]+ " " + b[4]+ " " + b[5]);
    }
    else //set a byte array
    {
    byte p = (byte)rand.nextInt(100);
    System.out.println(m+": Set " + l + " " + p);
    b[0] = p;
    b[1] = p;
    b[2] = p;
    b[3] = p;
    b[4] = p;
    b[5] = p;
    }


    }
    }
    }


    }
    --
    -----------------------------------------------------------------------
    | Rob Shepherd |
    | can be contacted privately through a temporary email account. |
    | Courtesy of http://www.mailinator.com |
    | |
    | |
    | |
    | This address will not be checked (by me) after May 31st 2004 |
    -----------------------------------------------------------------------
     
    Rob Shepherd, May 28, 2004
    #1
    1. Advertising

  2. Rob Shepherd

    Oscar kind Guest

    Rob Shepherd <> wrote:
    [rewrapped the text]
    > I wish to discover how i can make my threaded code safe with minimum of
    > synchronisation overhead. I would also like to treat each byte array with
    > separate synchronisation,

    [...]
    > I presume synchronised blocks are the way to go but is there a way of
    > doing it without making a get and set method for each byte array?


    Yes there is, but you have to sychronize in all pieces of code that write
    to the arrays:

    SynchroTest s;
    synchronize (s.c1)
    {
    s.c1[4] = 5;
    }

    A better approach IMHO would be to make the arrays in SynchroTest private,
    and create two methods to get resp. set the values in them. The setter
    then handles the synchronization, so the rest of your code doesn't have
    to.


    Oscar

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, May 28, 2004
    #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. Dirc Khan-Evans
    Replies:
    1
    Views:
    914
    Karl Seguin
    Oct 17, 2005
  2. avishosh
    Replies:
    2
    Views:
    10,624
    avishosh
    Aug 8, 2004
  3. Heinrich Moser
    Replies:
    1
    Views:
    466
    Heinrich Moser
    Mar 27, 2008
  4. Gabriel Rossetti
    Replies:
    0
    Views:
    1,363
    Gabriel Rossetti
    Aug 29, 2008
  5. John Nagle
    Replies:
    5
    Views:
    485
    John Nagle
    Mar 12, 2012
Loading...

Share This Page