• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization problem!!!

 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Well i ve a server that threads out a new object of the same as soon as it recvs a connection.. Well each of these threads have to write a value in xml format into a file. I have synchronized the method to write to xml.. but then the server threads still fail toi write into the file.. I guess it is some thing to do with mutual exclusion.. please guide me on how to re-design my server to make it run thread safe..
Regds
Gautham Kasinath
 
tom Su
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaus.. show us a little piece of your code.. maybe we could do some diggingggg
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Well here it is the code..

Well the Method critical section reads from a file and appends to the same.. because the file is an xml file I have to append before the closing highest tag.
Please enlighten me on this.. I aint got no work around.. i tried to use public synchronized void criticalSection to get over the same.. but if was futile..
Regds
Gautham Kasinath
 
ramesh gutty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gautham,
Here's the code that should answer your queries. lemme know if you have any further queries on this...
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.io.File;
public class TestMutex extends Thread {
private String strThreadName;
private String lock="";
private static RandomAccessFile file;
private final static String FILENAME="winner.xml";
public TestMutex(String threadName) {
strThreadName = threadName;
}
private void init() throws Exception {
System.out.println(strThreadName+": Opening file "+FILENAME);
file = new RandomAccessFile("winner.xml", "rw");
}
public void criticalSection() throws Exception {
String strData = strThreadName + ": username\r\n" + "\r\n";
//do whatever file I/O here, this example appends the username at the end of the file
file.seek(file.length());
file.write(strData.getBytes());
}
public void run() {
synchronized (lock) {
System.out.println(strThreadName+" is working with the critical section");
try {
if (file == null) init();
criticalSection();
}
catch (Exception exp) {
System.out.println(strThreadName+"Error in criticalSection for Thread: " + strThreadName);
}
}
}
public static void main(String a[]) {
for (int i = 0; i < 5; i++) {
new TestMutex("Thread: " + i).start();
}
}
public void finalize() {
closeFile();
}
public static void closeFile() {
try {
file.close();
} catch (Exception e) {
System.out.println("Error in closing file: " +FILENAME+": "+e);
}
}
}

Regards
Ramesh Gutty :-)
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramesh!
Well i tried running your code and it worked.. but alas when I edited your code to suit my needs it didnt work properly..
well I m re posting the edited code.. Here goes ..

well anyway the winner.xml file should read ..
winner
Thread 0 : username
Thread 1 : username
Thread 2 : username
...
/winner
Please note that after the execution of the first thread the winner.xml reads
winner
Thread 0 : username
/winner
and after the second the Thread 1 : username is appended after the previous "Thread 0 : username" and before </winner>
Regds
Gautham Kasinath
[This message has been edited by gautham kasinath (edited May 07, 2001).]
[This message has been edited by gautham kasinath (edited May 07, 2001).]
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! All!
Well I got round the problem that I faced.. Well the work around is simple.. I was using a random access file to read from a file till a specified tag into a string and to that adding the new string with the terminal tag.. and after that I was creating a temp file.. and deleting the odl file.. and renaming the temp to the old file..phew!!!
well that took the sweat outta me and the JVM and th os.. thus the Threads didnot behave themselves..
Anyway now I use a buffered Reader to read from the file.. and appen to a string with my new data.. and I use a printe writer object to write into the file..
well this time I ve synchronised only the writing part of the code.. and thus it runs thread safe to more that 50 simultaneous requests..
Thanks all for the help you extended.. it really was valuable to me.. Thanks esp Ramesh..
Regds
Gautham kasinath
P.S. If some one comes across similar problems please dont hesitate to mail me @ gkasinath@hotmail.com.. you will get a prompt solution.
 
ramesh gutty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gautham!
Great to know your problem was solved!! lemme know if you have any problems in future..
Regards,
Ramesh gutty
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Ramesh!
Well I truely appreciate your help!
I wouldnt have looked the other angle but for your help and the code that you sent me.
Thanks again
Regds
Gautham Kasinath
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic