简体   繁体   中英

Android thread sometimes doesn't start

So I'm having problems with my threads in an Android project. I have a ThreadStarter class, with a BuildScreen() function, which actually creates the layout for each activity. The only problem is, sometimes the threads just won't start, and I have no idea why. They work like 98% of the time though, but when they don't, the current activity will never get initalized, and the user has to restart the app, which is inconvenient.

Here is a snippet of my code:

   public class ThreadStarter
{
    public static void BuildScreen()
    {
        try
        {
            GlobalVariables.screenDrawer.onStart();
            GlobalVariables.listInitaliser.onStart();
            Logger.log("ThreadStarter.BuildScreen", "Threads started");
        }
        catch(IllegalThreadStateException e)
        {
            GlobalVariables.screenDrawer.StopThread();
            GlobalVariables.listInitaliser.StopThread();
            Logger.log("ThreadStarter.BuildScreen", "Threads stopped");

            GlobalVariables.screenDrawer.onStart();
            GlobalVariables.listInitaliser.onStart();
        }
        catch(Exception e)
        {
            Logger.Error("Couldn't stop or start the threads!");
            Logger.Error("Exception () Message: " + e.getMessage());
        }
    }
}

The threads:

    public class ListInitialiser extends Thread
{
    private static ListInitialiser _thread;
    public synchronized void run()
    {
        GlobalVariables.CurrentActivity.UpdateLists();

    }

    public  void onStart()
    {
        _thread = new ListInitialiser();
        _thread.start();
    }

    public void StopThread()
    {
        if (_thread != null)
        {
            _thread.interrupt();
            _thread = null;
        }
    }

}

I won't insert the ScreenDrawer thread here, because it's pretty much the same, except it calls another function.

And this is how every activity is created (of course the contentView differs in each file):

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().windowAnimations = R.style.Fade;
        setContentView(R.layout.activity_fine_data_3);
        GlobalVariables.CurrentActivity = this;
        ThreadStarter.BuildScreen();
        Logger.log("INFORMATION", "Person3DataActivity (Information 3/5)");
    }

In the GlobalVariables section I have these variables:

public static ScreenDrawer screenDrawer = new ScreenDrawer();
public static ListInitialiser listInitaliser = new ListInitialiser();

If anyone has a solution or and idea, please share it with me. Thanks in advance.


EDIT: Okay, so I took onof's (rather harsh but useful :)) advice, and refactored my code to use AsyncTask instead. It seems to be working pretty fine. I managed to implement it into my AbstractActivity class, which is the parent of every Activity I use, and now all I have to do is call BuildScreen() method in every onCreate method.

Thanks for the replies everyone.

try to add this to your class where u declared Global Variables

private static ListInitialiser instance;
public static synchronized ListInitialiser getInstance() {
    if (instance == null)
        instance = new ListInitialiser();
    return instance;
}

Everytime you donot have to create new when ur taking static.I dont know but may be this can help

You can't rely on static variables as everything that is static (non final) in Android can be cleared any time the system need memory. So don't think static = storage.

You should instead instantiate the objects when you need them, like following:

public static ScreenDrawer getScreenDrawer() {
    return new ScreenDrawer();
}

public static ListInitialiser getListInitialiser () {
    return new ListInitialiser ();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM