Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Thread safe resource

Reply
Thread Tools

Thread safe resource

 
 
Rob Shepherd
Guest
Posts: n/a
 
      05-28-2004
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 |
-----------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Oscar kind
Guest
Posts: n/a
 
      05-28-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
html5lib not thread safe. Is the Python SAX library thread-safe? John Nagle Python 5 03-12-2012 04:07 PM
os.ChDir() not thread-safe; was : Is tempfile.mkdtemp() thread-safe? Gabriel Rossetti Python 0 08-29-2008 08:30 AM
Very annoying error: Access to the path is denied. ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity Jay ASP .Net 2 08-20-2007 07:38 PM
Resource manager problem: naming for embedded resource. Dirc Khan-Evans ASP .Net 1 10-17-2005 12:52 PM
The system cannot locate the resource specified. Error processing resource avishosh XML 2 08-08-2004 06:28 AM



Advertisments