Thread problem!!!

I am a beginner in thread-programming and want to solve this problem:

a text-file should be copied every 10 sec. from folder A to folder B.
first the program should read a lock-file that indicates 1 or 0.
If the lock-file set to 1 the program should try for 10 sec. then exit else the program can copy the text-file from folder A to folder B (the program should set the lock-file to 1 before starting copy the text-file and after finishing to 0)

How many thread should be used??
any ideas ??
can somebody help


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.

>> If the lock-file set to 1 the program should try for 10 sec. then exit

Do you mean the program (thread) should *wait* for 10 seconds or that it should repeatedly read the lock-file and try to copy the file for 10 seconds?

The program only needs 1 thread to do this.
Use a TimerTask to do the copying if you want to schedule this to happen every 10 seconds.
If you have problems with your code, please post it, we'll be happy to help.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

>>The program only needs 1 thread to do this.

Of course, the Timer/TimerTask combo is using other threads behind the scenes...
...but this question smacks of homework ;-)
Hi CEHJ ;-)

I wondered where you'd got to.  Over half an hour :-O

I agree.  It's almost certainly a homework question, but to give him his due, aimueller has only asked for suggestions and not a full answer (so far) ;-)

Just for interest:

>> the Timer/TimerTask combo is using other threads behind the scenes

Does a Timer count as a thread or does it use OS facilities (where available)?
Timer uses threads internally
Thanx ;-)
Mick BarryJava DeveloperCommented:
> How many thread should be used??

Depends on how you want to handle if the process of copying takes >10secs.
If you want to perform the copy process every 10 seconds regardless of whether the previous copy has completed or not (which appears to be the case) then you will need to start a new Thread every 10 seconds.
So you will need one thread that starts a a new Thread every 10 seconds:

public void run()
   while (!stopped)
      new CopyThread(filelock).start();
   catch (Exception ex)

public class CopyThread extends Thread
   public void run()
      // do your copy here

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
aimuellerAuthor Commented:
Hi All,

this is the code that I use, it works fine on linux. It does the following:
while a.txt is set to 0
 wait 10 sec.
 try again
 and again
 till a.txt sets to 1

- The question should be the solution multithreaded?? since this application work only with one thread, it may block on some system (windows)

somebody has any tips how can the solution look like??



public class CopyFile {

  public static void main(String[] args) throws Exception{

    new Thread() {
      public void run() {
        boolean done = false;
        while( !done ) {
          if (CopyFile.readBlockFile("a.txt")) {
            done = true;
          } else {
            try {
            catch (InterruptedException e) {}

  public static boolean readBlockFile( String file) {
    String line = null;
    int num = 0;
    try {
      BufferedReader is = new BufferedReader(new FileReader(file));
      while ( (line = is.readLine()) != null) {
        num = Integer.parseInt(line);
    }catch(Exception e) {}
      if ( num == 1)
        return true;
      return false;

  public static void copyFile( String inFile, String outFile) {
    try {
      BufferedInputStream is = new BufferedInputStream(new FileInputStream(
      BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(
      int c;
      while (( c = != -1) {
    }catch(Exception e) {
      System.out.println("error: " + e.toString());


//------------------------ end--------------
Mick BarryJava DeveloperCommented:
Your solution is multithreaded, as is the suggestion I posted above.
Difference with the code I posted is that it starts a seperate thread to handle every copy, whereas your code handles all copies from the one thread.
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

From novice to tech pro — start learning today.