[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help with programming Threads

Posted on 2004-11-08
5
Medium Priority
?
202 Views
Last Modified: 2010-03-31
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...
0
Comment
Question by:emartin24
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 750 total points
ID: 12524255
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12524305
... 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
 

Author Comment

by:emartin24
ID: 12525902
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12527151
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12620173
8-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question