multithreading - JAVA : factorise code -> runnable anonymous class -


i factorise code because have huge same block of code in 2 class. in anonymous runnable class , here description of situation

class {     a(){}     do_a_function(){} } class b extends a{     b(){         runnable runb=new runnable() {             public void run() {                 while (true) {                     super.do_a_function();                     do_b_function();                                     }             }         };         new thread(runb).start();     }      do_b_function(){} } class c extends a{      c(){         runnable runc=new runnable() {             public void run() {                 while (true) {                     super.do_a_function();                     do_c_function();                                     }             }         };         new thread(runc).start();     }      do_c_function(){} } 

and can guess that

class {     a(){         runnable runc=new runnable() {             public void run() {                 while (true) {                     do_a_function();                     do_c_function() or do_b_function();                 }             }         };         new thread(runa).start();      }     do_a_function(){} }  class b extends a{     b(){         super();     }     do_b_function(){} }  class c extends a{     c(){         super();     }     do_c_function(){} } 

maybe argument in super() call , there no function pointers in java , , not want reflection if it's possible.

thank help

what this:

public class myrunnable implements runnable {     private final listener listener;      public myrunnable(listener listener) {         this.listener = listener;     }      public void run() {         while (true) {             doa();             listener.callback()         }     } }  public interface listener {     void callback(); }  class b implements listener {     b() {         new thread(new myrunnable(this)).start();     }      @override     public void callback() {         do_b_function();     }      public void do_b_function(){} } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -