简体   繁体   English

重新打开应用程序时,如何检查上一个会话中的线程是否仍在运行?

[英]How to check if a thread from a previous session is still running when I reopen the application?

My application is starting a continously running thread which runs a while(true){}. 我的应用程序正在启动一个持续运行的线程,该线程运行while(true){}。 When I reopen/reenter the application another thread is created and works concurrently with the previous. 当我重新打开/重新输入该应用程序时,将创建另一个线程,并且该线程与前一个线程同时工作。 I would like a mechanism which could check whether a thread from a previous session is still running and if it does then it shouldn't recreate it. 我希望有一种机制可以检查上一个会话中的线程是否仍在运行,如果运行,则不应重新创建它。 How to achieve that? 如何实现呢?

One way: Create a preference file with a simple boolean to say running or not. 一种方式:用一个简单的布尔值创建一个首选项文件,说是否运行。
http://developer.android.com/guide/topics/data/data-storage.html#pref http://developer.android.com/guide/topics/data/data-storage.html#pref

In your thread create a 在您的线程中创建一个

Boolean isRunning = false;

When your thread starts put it in the apps shared preference like this.. 当您的线程启动时,将其放入应用共享首选项,如下所示。

  isRunning = true;
  SharedPreferences settings = getSharedPreferences("isThreadRunning", 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("isRunning", isRunning);
  editor.commit();

Now when the thread is Finished just change it back to false. 现在,当线程完成时,只需将其更改回false。

isRunning = false;

In your activity just pull the boolean out of shared preference like this. 在您的活动中,像这样将布尔值从共享首选项中拉出来。

   SharedPreferences settings = getSharedPreferences("isThreadRunning", 0);
   boolean running = settings.getBoolean("silentMode", false); //if it doesnt exist it will automatically set to false which means the thread hasnt or isnt running

 then just test.

 if(running){
 //Do something
 }else{
 //the thread isnt running so lets start it.
 }

This is a very easy way to do it. 这是一种非常简单的方法。

Hope it helps 希望能帮助到你

I wrote a simple thread listing a while back. 我写了一个简单的线程,列出了一段时间。 user1031312 has a better solution, but here it is anyways in case it's useful to you. user1031312有一个更好的解决方案,但是无论如何它对您有用。 Not sure if it will list threads from a previous session though. 但不确定是否会列出上一个会话的线程。 It will list the thread names and id's in a TextView called thread_textview. 它将在名为thread_textview的TextView中列出线程名称和ID。

// This method recursively visits all thread groups under 'group'.
public static void visit(ThreadGroup group, int level) {
    // Get threads in 'group'
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads*2];
    numThreads = group.enumerate(threads, false);

    // Enumerate each thread in 'group'
    for (int i=0; i<numThreads; i++) {
        // Get thread
        thread_textview.append("-> "+threads[i].getName()+":"+threads[i].getId()+"\n");
    }

    // Get thread subgroups of 'group'
    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups*2];
    numGroups = group.enumerate(groups, false);

    // Recursively visit each subgroup
    for (int i=0; i<numGroups; i++) {
        visit(groups[i], level+1);
    }
}

You can use a ServerSocket to bind to a port. 您可以使用ServerSocket绑定到端口。 At one time the OS will only allow one thread to be bound. 一次,操作系统将仅允许绑定一个线程。 This will prevent the next one to not execute. 这样可以防止下一个不执行。

ServerSocket serverSocket = new ServerSocket(1234);

For next thread it will throw BindException. 对于下一个线程,它将抛出BindException。 So first line in your thread and it will do what you want. 因此,线程的第一行将执行您想要的操作。

ps 1234 is an unused port number. ps 1234是未使用的端口号。

Another option -- the above answer is probably not suitable to what you want so here is another option. 另一个选择-上面的答案可能不适合您想要的,因此这里是另一个选择。

public MyThreadFactory {

     List<String> runningThreads = new List<String>();

     public synchronized void startThread(MyThread thread) {
          if (! runningThreads.contains(thread.getID())) {
                runningThreads.add(thread.getID());
                // MyThread implements Runnable
                new Thread(thread).start();
          }
     }
}

assuming your thread object has getID method or something that uniquely identifies its type. 假设您的线程对象具有getID方法或唯一标识其类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重新打开应用程序时如何使单击的按钮仍然禁用 - How to make the button clicked still disable when reopen again application 如何使用从另一个线程收到的信息更新主UI? (虽然线程仍在运行) - How can I update the Main UI with information received from another thread? (While the thread is still running) 如果我知道线程名称,如何检查tomcat中的线程是否仍然存在? - How to check if a thread is still alive in tomcat if I know the thread name? 如何检查线程是否正在运行 - how to check thread is running 如何检查仍在使用哪个线程或对象? - How to check what thread or object is still used or not? 如何检查Android线程是否正在运行 - How to check if an Android Thread is running 如何从运行在另一个类中的TimerTask更新JavaFX应用程序线程? - How can I update the JavaFX application thread from a TimerTask running in a different class? 如何检查线程是否在ExecutorService线程池中运行 - How to check if a thread is running in the ExecutorService Thread pool 当后台线程运行时,如何在JavaFX应用程序中测试main方法? - How to test the main method in a JavaFX application when a background thread is running? 使用 java 重新打开一个已经运行的应用程序(.exe) - Reopen an already running application(.exe) using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM