Need help with programming Threads

Hello,

I have a program that uses a FTPClient to ftp to a remote server. While it is connected, there are a lot of transactions that need to take place and there is a risk that the connection could timeout...which would be bad. I need to create a thread that sends the NOOP command to the server (on an interval) while the processissing is going on and then when the processing is finished the thread will be joined/stopped/whatever...

Here is what I have:

In class Processor:
// Start new NoOp thread
ALNoop noop = new ALNoop(ftp);
Thread t = new Thread(noop);
t.start();

// Determine status and find waiting claims
PROCESSING GOES HERE

// Rejoin the NoOp thread
noop.finish();
if (t.isAlive()) {
  t.join();
}

In class ALNoop:
public class ALNoop extends Thread {
  FTPClient ftp = null;
  boolean keepRunning = true;

  public ALNoop(FTPClient ftp) {
    runningInstance = new ALNoop();
    this.ftp = ftp;
  }

  public void finish() {
    keepRunning = false;
    notify;
  }

  public void run() {
    keepRunning = true;
    while (keepRunning) {
      try {
        ftp.sendNoOp();
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      if (keepRunning) {
        try {
          Thread.currentThread().wait(5000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

The code above was just an attempt to get something working. I want the thread to be stopped once the processing is complete and I don't want to wait if the thread is currently sleeping or waiting. While attempting to do this, on the wait or notify (can't remember which) I have run into an error that says something about not the current owner of the thread.

Any suggestions...
emartin24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>I have run into an error that says something about not the current owner of the thread.

You need to call wait from a synchronized block
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
... and notify for that matter. This is similar to the classic producer/consumer problem. See

http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html
0
emartin24Author Commented:
I had previously read through the producer/consumer example, but still needed some direction. Can you explian/show an example of the synchronized block?
0
CEHJCommented:
Actually, i thought of an easier way - you can use a synchronized method to access the client and use that whenever you need it to do anything. A java.util.Timer could get a reference and send a NOOP. That way you don't need to worry about semaphores
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.