Java thread doubt -


consider program:

public class test {     public static void main(string [] args){         runnable r = new runnable() {             public void run() {                 system.out.print("foo");             }         };         thread t = new thread(r) {             public void run() {                 system.out.print("bar");             }         };         t.start();     } } 

on running output is

bar 

i know why so.

my understanding:

we instantiate anonymous inner class implements runnable overriding run method. , assign r.

next pass runnable thread class constructor. create new anonymous inner class extends thread overriding run.

so have 2 run methods , 2nd take preference on 1st.

i'm confused please correct me if i'm wrong.

also such crazy things used in real world java programming? (my prof says yes , thats keeps going :d).

though pass runnable instance constructor, you're overriding run method in such way, doesn't use r.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -