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

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

+5
source share
9 answers

You should not. Override run instead

+13
source

As others have said, redefinition Thread.start()is not a way to do this. Normally I would not override Thread.run(), but use Runnable.

If you need to decide which method to run after the thread has been spawned, you can 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();

, , , run() , late binding, :

Runnable couldBeAOrB = ...;
Thread thread = new Thread(couldBeAOrB);
thread.start();
+12

start

    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()
        }
    };

super.start(), run() . start ( ), .

run() start() ( ), , . Thread, - .

run(), , , ( , start), . , , run(), .

    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
            }
        }
    }

, , @Henning! - ...

+10

run() start() . .

, :

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

1: "t1.run()" "t2.run()" , t1 t2 ().

2. "t1.start()" "t2.start()" , run() t1 t2 ().

+4

, "run". :

new Thread() {
  public void run() {
    //your code here
  }
}.start();
//start will call the logic in your run method
+3

Schildmeijer, start, run().

, ( ), start0, , , run ( /). start0 , , , , .

, start(), (, ), , run , .

Sun (ahem, Oracle) http://download.oracle.com/javase/tutorial/essential/concurrency/index.html, .

+2

Worker Runnable {

public void run() {      ( "Foo" ) {       runFoo();     } else {       runBar();     }}

private void runFoo() {     // -}

private void runBar() {     //else }

}

, start-Method.

: java.util.concurrent.Callable

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Callable.html

+2

, start() .
, .

0

If we provide our own implementation of the start method, it will work like a regular method call and will only work with the current thread of the stack. A new thread will not be created.

-1
source

All Articles