java - Im losing my mind with threads -


i want objects of class:

public class chromosome implements runnable, comparable<chromosome> {     private string[] chromosome;     public double fitness;     private random chromogen;      public chromosome(double[] candidate) {         super();         //encode candidate per parameter; using matrix storage         chromosome = encode(candidate);         chromogen = new random();     }      //de-fault     public chromosome() {         super();         chromogen = new random();          //de-fault genotype         chromosome = new string[6];     }      /**      * implemented      */     public void run() {         //i hope leaving empty works...     }      public int compareto(chromosome c) {         return (int) (fitness - c.fitness);     }      /**      * fitness stored in chromosome!      */     public void setfitness(arraylist<double[]> target) {         fitness = ff.fitness(this, target);     }      public double getfitness() {         return fitness;     }      /**      * encoders/decoders      */     public string[] encode(double[] solution) {         //subtract 2^n solution until reach 2^-n          /**          * length: 51 bits!!          *          * 1st bit negative/positive          *          * precision [2^30 <-> 2^-20]!!!          *          * range: -2.14748...*10^9 <-> 2.14748...*10^9          */         string[] encoded = new string[6];          //per parameter         (int j = 0; (j < 6); j++) {             encoded[j] = encode(solution[j]);         }          return encoded;     }      public string encode(double sol) {         /**          * precision [2^30 <-> 2^-20]!!!          */         double temp = sol;         string row = "";         //negativity check         if (temp < 0) {             //negative             row = "1";         } else {             //positive             row = "0";         }         //main seq.         (int n = 30; (n > (-21)); n--) {             if ((temp - math.pow(2, n)) >= 0) {                 temp = temp - math.pow(2, n);                 row = row + "1";             } else {                 row = row + "0";             }         }         return row;     }      public double decoded(int position) {         //returns un-encoded solution         double decoded = 0.00;         char[] encoded = (chromosome[position]).tochararray();         /**          * [n?][<--int:30-->][.][<--ratio:20-->]          */         int n = 30;         (int = 1; (i < encoded.length); i++) {             if (encoded[i] == '1') {                 decoded += math.pow(2, n);             }             //next binary-place             n--;         }         //negative??         if (encoded[0] == '1') {             decoded = ((-1) * decoded);         }         //output         return decoded;     }      /**      * getters      * ---------------\/--redundant!!      */     public double getparameter(int parameter) {         //decoded solution         return decoded(parameter);     }      /**      * used in e-algrm.      */     public string getrow(int row) {         //encoded solution         return chromosome[row];     }      /**      * setters      */     public void setrow(string encoded, int row) {         chromosome[row] = encoded;     }      public void setrow(double decoded, int row) {         chromosome[row] = encode(decoded);     }      /**      * mutations      */     public void mutate(double mutationrate) {         //range of: 51         double ran = 0;         int r;         char[] temp;         (int m = 0; (m < 6); m++) {             temp = (chromosome[m]).tochararray();             ran = chromogen.nextdouble();             if (ran <= mutationrate) {                 r = chromogen.nextint(51);                 if (temp[r] == '1') {                     temp[r] = '0';                 } else {                     temp[r] = '1';                 }             }             //output             chromosome[m] = new string(temp);         }     } } 

...to in separate threads; have no need method run(); when try this:

child1 = new chromosome(); (new thread(child1)).start(); 

still, thread see @ run-time main(). so, how can make separate threads??

i see there problem in understanding of how threads work.

when create thread, looks method run(). there several ways of creating threads. doing passing runnable object.

thread t=new thread (new runnable);

do know how long thread lives?

  • a thread lives long method run() exists , runs. thread executes code inside run() method. not designed execute outside run(). thread dies when control moves out of run().

your case:

you left run() method empty. thread executes nothing , dies , when created.

what can do?

enclose rest of program in run() run() remains on heap , therefore, newly created thread runs program.

you don't need put everthing run(), can shift first method call run() remains on heap.

let take example:

public class threading implements runnable {      public static void main(string args[])      {        thread t = new thread (new runnable);         t.setname("thread1");         t.start();          print1();          print2();       }           public static void print2()       {       system.out.println(thread.getname());        }        public static void print1()       {       system.out.println(thread.getname());        }        public void run()        {       system.out.println(thread.getname());        } } 

ouputs:

  • thread1
  • main
  • main

time keep new thread alive till end.

public class threading implements runnable {      public static void main(string args[])      {        thread t = new thread (new runnable);         t.setname("thread1");         t.start();        }           public static void print2()       {       system.out.println(thread.getname());        }        public static void print1()       {       system.out.println(thread.getname());        print2();        }        public void run()        {       system.out.println(thread.getname());         print1();        } } 

output:

  • thread1
  • thread1
  • thread1

we kept method run() on heap putting method call in run(). method 1 further maintains flow.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -