问题
I want the objects of this 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 as 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 it 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 from solution until you reach 2^-n
/**
* LENGTH: 51 BITS!!
*
* 1st BIT IS NEGATIVE/POSITIVE
*
* THE PRECISION IS [2^30 <-> 2^-20]!!!
*
* RANGE: -2.14748...*10^9 <-> 2.14748...*10^9
*/
String[] encoded = new String[6];
//PER PARAMETER
for (int j = 0; (j < 6); j++) {
encoded[j] = encode(solution[j]);
}
return encoded;
}
public String encode(double sol) {
/**
* THE PRECISION IS [2^30 <-> 2^-20]!!!
*/
double temp = sol;
String row = "";
//NEGATIVITY CHECK
if (temp < 0) {
//negative
row = "1";
} else {
//positive
row = "0";
}
//main seq.
for (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;
for (int i = 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;
for (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 be in SEPARATE threads; however I have no need for the method run()
; but when I try to do this:
child1 = new Chromosome();
(new Thread(child1)).start();
Still, the only thread I see at run-time is main()
.
So, how can I make it separate threads??
回答1:
I see there is a problem in your understanding of how threads work.
When you create a thread, it looks for the method run()
. There are several ways of creating threads. I am doing it by passing a Runnable
object.
Thread t=new Thread (new Runnable);
Do you know how long a thread lives?
- A thread lives as long as the method
run()
exists and runs. A thread executes only the code inside therun()
method. It is not designed to execute anything outsiderun()
. The thread dies when the control moves out ofrun()
.
Your case:
You left the run()
method empty. So the thread executes nothing and dies as and when it is created.
What can you do?
Enclose the rest of the program in run()
so that run()
remains on the heap and therefore, the newly created thread runs the program.
You don't need to put everthing into run()
, you can simply shift the first method call to run()
so that it remains on the heap.
Let us take an 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 to keep your new thread alive till the 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 the method run()
on the heap by putting a method call in run()
. This method is the one which further maintains the flow.
回答2:
When you call Thread.start(), the new Thread will begin by execution of run() method, you should write what the thread has to do in run() method, after Thread finishes the run() method then it terminates, so it is no longer running or visible.
When you have empty run() method then it finishes instantly as is has nothing to do. What the thread should do, what should be invoked? just put that invocation into run() method.
Try this, maybe it will work as you expect, but i'm just guessing that you would like to move what you process in constructor into a thread.
public class Chromosome implements Runnable, Comparable<Chromosome>
{
private String[] chromosome;
public double fitness;
private Random chromoGen;
private double[] candidate;
public Chromosome(double[] candidate)
{
super();
this.candidate=candidate;
}
//De-fault
public Chromosome()
{
super();
}
/**
* IMPLEMENTED
*/
public void run()
{
if (candidate!=null) {
//encode candidate PER parameter; using Matrix as storage
chromosome = encode(candidate);
chromoGen = new Random();
} else {
chromoGen = new Random();
//de-fault genotype
chromosome = new String[6];
}
}
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 from solution until you reach 2^-n
/**
* LENGTH: 51 BITS!!
*
* 1st BIT IS NEGATIVE/POSITIVE
*
* THE PRECISION IS [2^30 <-> 2^-20]!!!
*
* RANGE: -2.14748...*10^9 <-> 2.14748...*10^9
*/
String[] encoded = new String[6];
//PER PARAMETER
for (int j = 0; (j < 6); j++){
encoded[j] = encode(solution[j]);
}
return encoded;
}
public String encode(double sol)
{
/**
* THE PRECISION IS [2^30 <-> 2^-20]!!!
*/
double temp = sol;
String row = "";
//NEGATIVITY CHECK
if (temp < 0){
//negative
row = "1";
}
else{
//positive
row = "0";
}
//main seq.
for (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;
for (int i = 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;
for (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);
}
}
}
回答3:
I think your thread fine and must work, but your run
method is empty and the thread get terminated without you noticing it
Try this:
public void run()
{
System.out.println("I hope leaving it empty works...");
}
and if you see the Message in the console
, then everything is fine and you need only to add a condition/loop/logic inside the run
method so that is not getting terminated before you do the work done.
来源:https://stackoverflow.com/questions/35837458/im-losing-my-mind-with-threads