Search code examples
javamultithreadingrestart

Java Threads: Unable to create new Thread and killing / interrupting old Thread


im kind of new to java threads and ive just ran into a problem with a group project:

//edit: the program is a JFrame which needs a kind of "relaunch" to load the propertie file changes.

We've got a program where you are able to change some properties. For the changes to take effect, the program needs a restart / new launch with new JVM (seems to me?)

The problem is following:

I've already made a Thread which actualy starts our program and later on, the change will trigger the program to create a new thread. This is working, but im unable to kill the old thread. And if i try to do it ("X" or programmatically) i kill both instances at the same time. So there're actualy no 2 threads? - otherwise they should be treated seperatly, shoudln't they?

On the other hand when using Thread.currentThread().interrupt() both instances will remain, but im unable to do literally anything.

Thread starter:

public static void createNewInstance() {
prog = new Runnable() {
        @Override
        public void run() {
            try {
                 //loading propertie data into String[] array..
                 Start.main(String[] array);
                 while (true)  {
                        if (!isRunning) {
                            currentThread().interrupt();
                            createNewInstance();
                            isRunning = !isRunning;
                        }
                 }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    };
    new Thread(prog).start();
}

and at the change event:

        MyThread.isRunning = false;
        Thread.currentThread().interrupt();

As this is new to me and i can't figure out how i should achieve this im thankful for any kind of advice and any kind of mistake i've made so far. (Sorry for any kind of mistakes - im not a native speaker)

Thanks in advance!

----Workaround solution:----

In my case the propertie change was related to a new language setting: DE ->EN
My Messages:
public class Messages { private static String bundle_name = "com.ttr.language.messages" + new PropertiesClass().getProperty("lang"); //$NON-NLS-1$ private static ResourceBundle resource_bundle = ResourceBundle.getBundle(bundle_name);

Then I added this method to Messages class:

public static void updateProperties(String language) { bundle_name = "com.ttr.language.messages" + language; resource_bundle = ResourceBundle.getBundle(bundle_name); }

And used it in my program:

props.setProperty("lang", "EN"); <- example Messages.updateProperties(props.getProperty("lang"));
//dispose window and start login


Solution

  • Let's say XThread is the thread which implements your functionality.When a change is triggered you could call terminateThread() to stop the previous running thread and call getInstance() to run the new thread with new properties.

     class XThread implements Runnable{
    
            private static Thread rT = null;
            private XThread()
            {}
            public static void terminateThread()
            {
                rT = null;
            }
    
            public static Thread getInstance()
            {
                if(rT==null)
                {
                    rT = new Thread(new XThread());
                }
                return rT;
            }
    
            public void run()
            {
             //whatever functionality you want to add
            }
        }
    

    Hope this helps :)