Threads, Accessing shared resource


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Threads, Accessing shared resource

  1. #1
    Join Date
    Mar 2006
    Posts
    12

    Unhappy Threads, Accessing shared resource

    Hi,
    I've written a client-server program, the client inputs data, and the server accepts that data and writes it into a text file(file gets created when program is run). Works fine for a single client, now if I have 2 instances of the client running, i want the server to write the data input by client1 and client2 into the text file, what happens in my program is, client1's data is overwritten by client2's data. please can you help me correct this error.

    Thanks,
    Rohan.
    -------------------------
    SocketClient file:

    import java.net.*;
    import java.io.*;

    public class SocketClient {
    public static void main(String[] args) {
    String host = "localhost";
    int port = 19999;
    System.out.println("SocketClient initialized");
    try {
    InetAddress address = InetAddress.getByName(host);
    Socket connection = new Socket(address, port);
    try {
    while(true){
    PrintWriter output = new PrintWriter(connection.getOutputStream(),true);
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter Query: ");
    String lineToBeSent = input.readLine();
    output.println(lineToBeSent);
    }
    }
    catch (IOException e) {
    System.out.println(e);
    }

    try{
    connection.close();
    }catch(IOException e){
    System.out.println(e);
    }
    }
    catch (IOException f) {
    System.out.println("IOException: " + f);
    }

    }
    }
    ---------------------------------------------------------
    MultipleSocketServer file:

    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class MultipleSocketServer implements Runnable {

    private Socket connection;
    private String TimeStamp;
    private int ID;
    public static void main(String[] args) {
    int port = 19999;
    int count = 0;
    try{
    ServerSocket socket1 = new ServerSocket(port);
    System.out.println("MultipleSocketServer Initialized");
    while (true) {
    Socket connection = socket1.accept();
    Runnable runnable = new MultipleSocketServer(connection, ++count);
    Thread thread = new Thread(runnable);
    thread.start();
    }
    }
    catch (Exception e) {}
    }
    MultipleSocketServer(Socket s, int i) {
    this.connection = s;
    this.ID = i;
    }
    public void run() {

    try {
    BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    PrintStream out1 =new PrintStream(new FileOutputStream("test.txt"));

    while(true) {
    String message = input.readLine();
    out1.println(message);
    out1.println();
    System.out.println("Record Added to file");
    }
    }
    catch (IOException e) {
    System.out.println(e);
    }

    finally {
    try {
    connection.close();
    }
    catch (IOException e){}
    }
    }
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    Your first problem is the way you open the textfile:
    PrintStream out1 =new PrintStream(new FileOutputStream("test.txt"));
    using new FileOutputStream("test.txt") will shurely create / overwrite the existing file.
    But using new FileOutputStream("test.txt", true), which enables appending to the existing file will also make problems, when two seperate threads access the same file (while one thread is writing, the second thread can interrupt and append its own content)
    what you need is a single synchronized method accessing one file:

    Code:
    public static final PrintStream log = new PrintStream(new FileOutputStream("test.txt", true));
    public static synchronized final log(String message){
       log.println(message);
       log.println();
    }
    since the PrintStream log is static it will exist only once. and since the accessmethod log is synchronized, it will prevent to concurring threads to disturb each other.
    well, at this point you will have to write also a method that will properly flush and close the log file after all threads are down and the aplication is about to exit.

Similar Threads

  1. Re: App Object (fixes)
    By Rob Teixeira in forum .NET
    Replies: 129
    Last Post: 06-06-2002, 05:23 AM
  2. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 03:30 PM
  3. Replies: 1
    Last Post: 01-23-2002, 09:10 AM
  4. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM
  5. Replies: 0
    Last Post: 04-26-2001, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center