1

I have written the following method in a class that inherits from the Thread class:

protected File createFile(String fileName){
        try {
            File file = new File(fileName);
            if(!file.exists())
                file.createNewFile();
            else{
                file.delete();
                file.createNewFile();
            }
            file.mkdirs();
            return file;
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

It will be called from an Activity once the Thread has already started. If I do this from my Activity:

File logFile = mThread.createFile("/Logs/test.txt");
if(logFile!=null)
//do something
else
//do something else

Will it block until the file is created?

Swayam
  • 16,294
  • 14
  • 64
  • 102
Moises Jimenez
  • 1,962
  • 3
  • 21
  • 43
  • 3
    It will run on the calling thread - if you call it from the UI thread, it will block your activity, if you call it from the thread you created, it will run on that thread. – assylias Aug 29 '12 at 09:08
  • Are you following the Thread documentation? (http://developer.android.com/guide/components/processes-and-threads.html) If your just starting up, use AsyncTask instead (http://developer.android.com/reference/android/os/AsyncTask.html) its clearer what runs where.. – Roman Aug 29 '12 at 09:20
  • Did you check it for yourself before posting the question? – Andro Selva Aug 29 '12 at 09:25
  • Yes and it did create the file but I was wondering if the main activity was being blocked momentarily. I have used AsyncThread before but I'm trying to write a single thread that runs indefinitely and only does IO actions when its methods are called from the activity. – Moises Jimenez Aug 29 '12 at 09:30

2 Answers2

1

Yes it will block.

What counts is not the class from which the method is called, it is the run() method of the thread from which it is called. There is a high probability that

File logFile = mThread.createFile("/Logs/test.txt");
if(logFile!=null)
//do something
else
//do something else

is called from your activity run method (handled by android), and calling directly a method from another thread class will not by magic make this thread run this method.

To ensure that you are not blocking your activity with this createFile method you should use message passing mechanism so that createFile is called within the run() method of your non-activity thread.

See examples like the ones in this post for how to pass messages between threads. To sum up your activity thread will set a variable from your other thread. This thread periodically check this variable and execute actions depending on it. That's the main idea but there is a lot to read on the web about this for thread safety and other problems that arise in these cases.

Community
  • 1
  • 1
jolivier
  • 7,380
  • 3
  • 29
  • 47
0

Your Activity is a set of callback methods that get invoked by Android on the UI thread. These callbacks get executed during the lifecycle of the Activity (as described in detail in the doc). While one callback is executing, no other UI-related callbacks will.

If any one call back takes a long time, then the UI stops responding (which you'll often see referred to as "the UI being blocked").

To answer the actual question: yes, the UI thread will stop processing other events while it is waiting for your callback to complete, including the call to the createFile method.

I am assuming here (since it is not clear from the question) that the call to createFile is in one of the Activity lifecycle callback methods (like onCreate) or a UI event callback related to the Activity (like an OnClickListener attached to a Button on the Activity).


If you do not want to hold up the UI thread while creating the file then you will need to perform call createFile from a different "background" thread.

Note that if you need to make updates to the UI from your "background" thread, you must make sure to schedule the calls into the Android UI APIs to be made back on the UI thread.

This can be done using Thread and Handler objects, but it is often more convenient to use an AsyncTask as it offers a convenient framework of methods that get called on the right threads for the background task and UI updates.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46