Current Position:Home > Problem with threads...

Problem with threads...

Update:10-11Source: network consolidation
Advertisement
HI friends...
I am trying to make a server with a logging facility...
Let me explain the schema of the process...
1. the server starts as a thread and listens on port 9999 for any client.
2. when a client is accepted a PROCESS thread is started and the server gets back to listening.
3. now the PROCESS thread starts a initializes a LOGGER and listens the inputstream for any message from the client. the LOGGER initializes a log file and when a message is recieved it writes to the log file.
4. when a second client is connected to the server steps 2 and 3 take place.
but now the problem is ... when the second client connects to the server, the logger stops writing the messages of the first client to log1 but continues writing the messages of client1 & client2 to log2.
please give me a solution for this.
i can send you the project if you want to take a look at it.
thanx in advance...

The Best Answer

Advertisement
hi friends i thought i would send the code to your personal email when wanted so i didnot paste it here...
anyway i made a test project where i am having the same problem..
Test.java
package testproject;
public class Test {
  public Test() {
    for(int i = 0; i< 5; i++){
      TestThread thread = new TestThread();
      thread.start();
  public static void main(String[] args){
    new  Test();
TestThread.java
package testproject;
public class TestThread extends Thread{
  public TestThread() {
  public void run(){
    System.out.println(this.getName());
    Logger logger = new Logger(this.getName());
    while(true){
      logger.writeLog(this.getName());
      try {
        sleep(100);
      catch (InterruptedException ex) {
Logger.java:
package testproject;
import java.io.*;
public class Logger {
  File logfile;
  public static BufferedWriter FilewriteLOG;
  public Logger(String dir) {
    makeDir(dir);
    System.out.println("log: " + logfile);
    try {
      FilewriteLOG = new BufferedWriter(new FileWriter(logfile));
    catch (IOException ex) {
  public static void writeLog(String str) {
    try {
      FilewriteLOG.write(str + "\n");
      FilewriteLOG.flush();
    catch (IOException ex) {
      ex.printStackTrace();
  private void makeDir(String clientID) {
    String dir = "logs/" + clientID;
    String file = System.currentTimeMillis() + "";
    boolean exist = (new File(dir)).exists();
    if (exist) {
      makeFile(dir, file);
    else {
      new File(dir).mkdir();
      makeFile(dir, file);
  private void makeFile(String dir, String fileName) {
    logfile = new File(dir + "/" + fileName + ".txt");
    try {
      logfile.createNewFile();
      FilewriteLOG = new BufferedWriter(new FileWriter(logfile));
    catch (IOException ex) {
  • Problem with Thread and InputStream Update:10-11

    Hi, I am having a problem with threads and InputStreams. I have a class which extends Thread. I have created and started four instances of this class. But only one instance finishes its' work. When I check the state of other three threads their state

  • A problem with threads Update:10-11

    I am trying to implement some kind of a server listening for requests. The listener part of the app, is a daemon thread that listens for connections and instantiates a handling daemon thread once it gets some. However, my problem is that i must be ab

  • A problem with Threads and MMapi Update:10-11

    I am tring to execute a class based on Game canvas. The problem begin when I try to Play both a MIDI tone and to run an infinit Thread loop. The MIDI tone "Stammers". How to over come the problem? Thanks in advance Kobi See Code example below: i

  • J2ME problem with threads Update:10-11

    Hi all, I would like to ask you for a help. I need to write a small program at my university. I started to write a midlet which function would be to countdown time for sports activities. I woul like to start a new thread - the one that counts down -

  • Problem with threads within applet Update:10-11

    Hello, I got an applet, inside this applet I have a singleton, inside this singleton I have a thread. this thread is running in endless loop. he is doing something and go to sleep on and on. the problem is, when I refresh my IE6 browser I see more th

  • Problem with Threads and a static variable Update:10-11

    I have a problem with the code below. I am yet to make sure that I understand the problem. Correct me if I am wrong please. Code functionality: A timer calls SetState every second. It sets the state and sets boolean variable "changed" to true. T

  • Problem with threads running javaw Update:10-11

    Hi, Having a problem with multi thread programming using client server sockets. The program works find when starting the the application in a console using java muti.java , but when using javaw multi.java the program doesnt die and have to kill it in

  • Problem with Threads and "plase wait..."-Window Update:10-11

    Hi everyone, I have a problem that I'm not able to solve in any way... I have a time-consuming task (a file decryption) which I execute in a separate thread; I've used the SwingWorker class, like suggested by sun-tutorial, and it works right. The pro

  • Problem with threads in j2me Update:10-11

    im using jdk 1.6 with java wtk 2.1 ya i know.. 2.1 is a jdk1.4 source but im using souce as 1.4 during compiliation so thats not the prob this is my code package org.learn; import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Aler

  • Problem with threads and camera. Update:10-11

    Hi everybody! I've a problem with taking snapshot. I would like to display a loading screen after it take snapshot ( sometimes i have to wait few seconds after i took snapshot. Propably photo is being taken in time where i have to wait). I was trying