0

I'm trying to create my first server, a simple chat server.

I have a Runnable class called cCleanThread that creates a thread that uses sleep to call cGlobals.mUserList.Clean(); every 10 seconds.

I would like to have it so each instance of cCleanThread would have its own mUserList object. I can't figure out a easy way to do this.

I figured I would somw how have to pass in a parameter for a mUserList when I create my cCleanThread object?

code

public class cCleanThread   implements Runnable {

Thread runner;
public cCleanThread() {
}

public cCleanThread(String threadName) {
    runner = new Thread(this, threadName); // (1) Create a new thread.
    System.out.println(runner.getName());
    runner.start(); // (2) Start the thread.
}

public void run() {
    //Display info about this particular thread
    System.out.println(Thread.currentThread());
     while(true)
     {

         try {
            Thread.sleep(20*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         cGlobals.mUserList.Clean(); 
     }
}
}
Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • possible duplicate of [how can I pass a variable into a new Runnable declaration?](http://stackoverflow.com/questions/4297261/how-can-i-pass-a-variable-into-a-new-runnable-declaration) – Ralph Nov 28 '11 at 17:51

4 Answers4

2

This is not an answer. It is more like an improvement.

If you want to run clean every 10 second, you can use Timer class. This is the example. The thread is done automatically for you.

gigadot
  • 8,879
  • 7
  • 35
  • 51
1

Add mUserList as instance variable in cCleanThread class and initialize it in the constructor. Since every thread corresponds to an instance of cCleanThread instance, there will be exactly one mUserList for each thread. This mUserList instance will be accessible to your run() method since they'll both be in the same class.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
1

Pass the mUserList to the thread constructor:

public class cCleanThread implements Runnable {

    private final UserList localUserList;

    public cCleanThread(String threadName, UserList mUserList) {
        this.localUserList = mUserList;
        //...
    }

    public void run() {
        //...
        localUserList.Clean(); 
    }
}

The simply create thread with different arguments:

Thread first  = new cCleanThread("Thread-Foo", fooUsers);
Thread second = new cCleanThread("Thread-Bar", barUsers);
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
0

Take a look at the SceduledExecutorService: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)

And take a further look at the new Threadsafe-Collections: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingDeque.html

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79