Thread safe resource

R

Rob Shepherd

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 |
| |
| (e-mail address removed) |
| |
| This address will not be checked (by me) after May 31st 2004 |
-----------------------------------------------------------------------
 
O

Oscar kind

Rob Shepherd said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top