how to override thread.start() method in java?

前端 未结 10 1767
情歌与酒
情歌与酒 2020-12-03 02:07

I need to implement thread.start() method in my java code. Please let me know through an example of overriding of thread.start() method and how it works?

相关标签:
10条回答
  • 2020-12-03 02:40

    You can override start as any other method

        Thread myThread = new Thread() {
    
            @Override
            public void start() {
                // do something in the actual (old) thread
                super.start();
            }
    
            @Override
            public void run() {
                // do something in a new thread if 'called' by super.start()
            }
        };
    

    but you must call super.start() to create a new thread and have run() called in that new thread. The original start does some magic (native code) that you hardly can mimic.

    If you call run() directly from within your start() (or any other method), it is executed in the actual thread as a normal method, not in a new thread. There is no reason to use a Thread if you don't want to run some code in a new thread.

    You must put your decision logic in the run() method, maybe using some variable set in the constructor (or another method, eventually in start) if that is really needed. I can not find any reason for needing this variable, it should be enough to test the condition in run() as already suggested elsewhere.

        class MyThread extends Thread {
    
            private final boolean flag;
    
            public MyThread(boolean someCondition) {
                flag = someCondition;
            }
    
        // alternative  
        //    @Override
        //    public synchronized void start() {
        //        flag = <<someCondition>>
        //        super.start();
        //    }
    
            @Override
            public void run() {
                if (flag) {
                    // do something like super.run()
                } else {
                    // do something else
                }
            }
        }
    

    but it would be easier to understand and maintain if you do it like @Henning suggested!
    It's also a more object oriented solution...

    0 讨论(0)
  • 2020-12-03 02:41

    You should not. Override run instead

    0 讨论(0)
  • 2020-12-03 02:45

    Yes the start() method can be overridden.
    But it should not be overridden as it is implementation in thread class has the code to create a new executable thread and is specialised.

    0 讨论(0)
  • 2020-12-03 02:50

    As others said, overriding Thread.start() is not the way to do it. Usually, I wouldn't override Thread.run() either, but use a Runnable.

    If you have to decide which method to run after the thread has been spawned, you could do something like this:

    final Runnable runnableA = ...;
    final Runnable runnableB = ...;
    
    Runnable r = new Runnable() {
        @Override
        public void run() {
            if (...) {
                runnableA.run();
            } else {
                runnableB.run();
            }
        }
    }
    
    Thread thread = new Thread(r);
    thread.start();
    

    If, as you say, you have a superclass and a subclass where the run() method is overidden, you can just rely on late binding and the proper method will be invoked automatically:

    Runnable couldBeAOrB = ...;
    Thread thread = new Thread(couldBeAOrB);
    thread.start();
    
    0 讨论(0)
  • 2020-12-03 02:54

    Actually, you can call run() to run a thread instead of start() to run a thread. But there is a little difference.

    Suppose you create two threads:

    Thread t1 = new Thread();
    Thread t2 = new Thread();
    

    Case 1 : If you call "t1.run()" and "t2.run()" one after another they will start to run t1 and t2 synchronously (sequentially).

    Case 2 : If you call "t1.start()" and "t2.start()" one after another they will call their run() methods and start to run t1 and t2 asynchronously (in parallel).

    0 讨论(0)
  • 2020-12-03 02:54

    If we provide our own implementation of start method then it will work like a normal method call and will work on the current thread stack only. New thread will not be created.

    0 讨论(0)
提交回复
热议问题