Results 1 to 4 of 4
  1. #1
    dek_vault is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default [Solved] Thread and exception problem with two classes

    Hi,

    I have two classes, where main class is simple with only main function in it. Another class extends Thread and there's couple of functions I want to execute from main class.

    Problem: I try to get other.shut() to be run in main class catch() block, after I have stopped the other class's running thread (e.g. by ctrl+c).
    I think I need to somehow "pass" the exception from other class to main class so it goes to the catch block?


    Code for main class:

    Java Code:
    public class MainClass {
     
        public static void main(String[] arguments) {
           
            OtherClass other = new OtherClass();
           
            try {
                other.exec();
            } catch (Exception e) {
                other.shut();
            }
           
        }    
    }
    Code for other class:

    Java Code:
    public class OtherClass extends Thread {
     
        public OtherClass() {
            System.out.println("Created OtherClass.");
           
        }
     
        @Override
        public void run() {
            System.out.println("Running OtherClass thread..");
     
            long lastTime = System.currentTimeMillis();
     
            try {
     
                while (true) {
                    if (System.currentTimeMillis() - lastTime > 1000L) {
                        System.out.print(".");
                        lastTime = System.currentTimeMillis();
                    }
                }
     
            } catch (Exception e) {
                e.printStackTrace();
            }
     
        }
     
        public void exec() {
            System.out.println("Starting OtherClass thread..");        
            start();
        }
     
        public void shut() {
            System.out.println("OtherClass shut down.");
           
            // Write some logging data to file here..
        }
    }
    Regards,
    dek
    Last edited by dek_vault; 01-23-2014 at 11:16 AM. Reason: solved the problem

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Thread and exception problem with two classes

    Problem: I try to get other.shut() to be run in main class catch() block,
    There needs to be an exception thrown for code in a catch block to be executed. Does the exec() method throw an exception?

    Catching an exception thrown in another Thread will require more complicated logic. One way would be a callback method that the thread could call. Another technique could use the wait() and notify() methods.

    What is printed on the console when the code executes?
    Last edited by Norm; 01-23-2014 at 03:33 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dek_vault is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: Thread and exception problem with two classes

    Thanks for the answer!

    I managed to get this working by adding a shutdown hook like so:

    Java Code:
    final Thread mainThread = Thread.currentThread();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            keepRunning = false;
            mainThread.join();
        }
    });

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Thread and exception problem with two classes

    Interesting solution. But I think I'd just move the shutdown logic to the end of the run() of the second thread, and then simply signal it to break out of its loop by either making it possible to interrupt() it, or by making the shut() method set a simple boolean to false.

    Java Code:
    private boolean running = true;
    
    public void run() {
      while(running){
         // do things and stuff
      }
    
      // do shutdown logic
    }
    
    public void shut(){
      running = false;
    }
    Btw: you probably want to add some Thread.sleep() logic to it so it isn't an busy loop that eats up an entire CPU by doing absolutely nothing all the time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 6
    Last Post: 04-19-2012, 08:10 PM
  2. Replies: 1
    Last Post: 11-06-2011, 05:41 PM
  3. Replies: 2
    Last Post: 11-06-2011, 05:40 PM
  4. Replies: 6
    Last Post: 12-04-2010, 09:36 AM
  5. Replies: 3
    Last Post: 11-06-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •