Solved

Thread problem!!!

Posted on 2003-11-05
14
197 Views
Last Modified: 2010-03-31
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

regards

andreas
0
Comment
Question by:aimueller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
14 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9687136
>> 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.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9687156
Use a TimerTask to do the copying if you want to schedule this to happen every 10 seconds.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9687201
If you have problems with your code, please post it, we'll be happy to help.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9687445
>>The program only needs 1 thread to do this.

Of course, the Timer/TimerTask combo is using other threads behind the scenes...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9687451
...but this question smacks of homework ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9687485
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)?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9687720
Timer uses threads internally
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9687733
Thanx ;-)
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 9691208
> 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)
   {
   try
   {
      new CopyThread(filelock).start();
      Thread.sleep(10000);
   }
   catch (Exception ex)
   {
      ex.printStackTrace();
   }
   }
}


public class CopyThread extends Thread
{
   ...
   public void run()
   {
      // do your copy here
   }
}
0
 

Author Comment

by:aimueller
ID: 9696754
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??

thanks

Andreas
 
//-----------------begin-----------------------------------------------
import java.io.*;

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")) {
            copyFile("a.gif","b.gif");
            done = true;
          } else {
            try {
              System.out.println("waiting..");
              Thread.sleep(10000);
            }
            catch (InterruptedException e) {}
          }
        }
      }
    }.start();
  }

  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);
      }
      is.close();
    }catch(Exception e) {}
      if ( num == 1)
        return true;
      else
      return false;
  }


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

  }

}
//------------------------ end--------------
0
 
LVL 92

Expert Comment

by:objects
ID: 9697083
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.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a simpler dropbox system? 10 53
Java Eclipse Loop 3 50
batch vs regular insert in spring DAO 2 55
junit initializtion error 2 22
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

710 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